mirror of
https://sourceware.org/git/glibc.git
synced 2024-12-26 04:31:03 +00:00
2e0bbbfbf9
The reallocarray function is an extension from OpenBSD. It is an integer-overflow-safe replacement for realloc(p, X*Y) and malloc(X*Y) (realloc(NULL, X*Y)). It can therefore help in preventing certain security issues in code. This is an updated version of a patch originally submitted by Rüdiger Sonderfeld in May 2014 [1]. Checked on i686-linux-gnu and x86_64-linux-gnu. [1] <https://sourceware.org/ml/libc-alpha/2014-05/msg00481.html>. 2017-05-30 Dennis Wölfing <denniswoelfing@gmx.de> Rüdiger Sonderfeld <ruediger@c-plusplus.de> * include/stdlib.h (__libc_reallocarray): New declaration. * malloc/Makefile (routines): Add reallocarray. (tests): Add tst-reallocarray.c. * malloc/Versions: Add reallocarray and __libc_reallocarray. * malloc/malloc-internal.h (check_mul_overflow_size_t): New inline function. * malloc/malloc.h (reallocarray): New declaration. * stdlib/stdlib.h (reallocarray): Likewise. * malloc/reallocarray.c: New file. * malloc/tst-reallocarray.c: New test file. * manual/memory.texi: Document reallocarray. * sysdeps/unix/sysv/linux/aarch64/libc.abilist: Add reallocarray. * sysdeps/unix/sysv/linux/alpha/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/arm/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/hppa/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/i386/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/ia64/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/microblaze/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/nios2/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/powerpc/powerpc64/libc-le.abilist: Likewise. * sysdeps/unix/sysv/linux/powerpc/powerpc64/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/sh/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/tilepro/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/x86_64/64/libc.abilist: Likewise. * sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist: Likewise.
105 lines
4.1 KiB
C
105 lines
4.1 KiB
C
/* Internal declarations for malloc, for use within libc.
|
|
Copyright (C) 2016-2017 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; see the file COPYING.LIB. If
|
|
not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
#ifndef _MALLOC_INTERNAL_H
|
|
#define _MALLOC_INTERNAL_H
|
|
|
|
#include <malloc-machine.h>
|
|
#include <malloc-sysdep.h>
|
|
|
|
/* INTERNAL_SIZE_T is the word-size used for internal bookkeeping of
|
|
chunk sizes.
|
|
|
|
The default version is the same as size_t.
|
|
|
|
While not strictly necessary, it is best to define this as an
|
|
unsigned type, even if size_t is a signed type. This may avoid some
|
|
artificial size limitations on some systems.
|
|
|
|
On a 64-bit machine, you may be able to reduce malloc overhead by
|
|
defining INTERNAL_SIZE_T to be a 32 bit `unsigned int' at the
|
|
expense of not being able to handle more than 2^32 of malloced
|
|
space. If this limitation is acceptable, you are encouraged to set
|
|
this unless you are on a platform requiring 16byte alignments. In
|
|
this case the alignment requirements turn out to negate any
|
|
potential advantages of decreasing size_t word size.
|
|
|
|
Implementors: Beware of the possible combinations of:
|
|
- INTERNAL_SIZE_T might be signed or unsigned, might be 32 or 64 bits,
|
|
and might be the same width as int or as long
|
|
- size_t might have different width and signedness as INTERNAL_SIZE_T
|
|
- int and long might be 32 or 64 bits, and might be the same width
|
|
|
|
To deal with this, most comparisons and difference computations
|
|
among INTERNAL_SIZE_Ts should cast them to unsigned long, being
|
|
aware of the fact that casting an unsigned int to a wider long does
|
|
not sign-extend. (This also makes checking for negative numbers
|
|
awkward.) Some of these casts result in harmless compiler warnings
|
|
on some systems. */
|
|
#ifndef INTERNAL_SIZE_T
|
|
# define INTERNAL_SIZE_T size_t
|
|
#endif
|
|
|
|
/* The corresponding word size. */
|
|
#define SIZE_SZ (sizeof (INTERNAL_SIZE_T))
|
|
|
|
/* MALLOC_ALIGNMENT is the minimum alignment for malloc'ed chunks. It
|
|
must be a power of two at least 2 * SIZE_SZ, even on machines for
|
|
which smaller alignments would suffice. It may be defined as larger
|
|
than this though. Note however that code and data structures are
|
|
optimized for the case of 8-byte alignment. */
|
|
#ifndef MALLOC_ALIGNMENT
|
|
# define MALLOC_ALIGNMENT (2 * SIZE_SZ < __alignof__ (long double) \
|
|
? __alignof__ (long double) : 2 * SIZE_SZ)
|
|
#endif
|
|
|
|
/* The corresponding bit mask value. */
|
|
#define MALLOC_ALIGN_MASK (MALLOC_ALIGNMENT - 1)
|
|
|
|
|
|
/* Called in the parent process before a fork. */
|
|
void __malloc_fork_lock_parent (void) internal_function attribute_hidden;
|
|
|
|
/* Called in the parent process after a fork. */
|
|
void __malloc_fork_unlock_parent (void) internal_function attribute_hidden;
|
|
|
|
/* Called in the child process after a fork. */
|
|
void __malloc_fork_unlock_child (void) internal_function attribute_hidden;
|
|
|
|
/* Set *RESULT to LEFT * RIGHT. Return true if the multiplication
|
|
overflowed. */
|
|
static inline bool
|
|
check_mul_overflow_size_t (size_t left, size_t right, size_t *result)
|
|
{
|
|
#if __GNUC__ >= 5
|
|
return __builtin_mul_overflow (left, right, result);
|
|
#else
|
|
/* size_t is unsigned so the behavior on overflow is defined. */
|
|
*result = left * right;
|
|
size_t half_size_t = ((size_t) 1) << (8 * sizeof (size_t) / 2);
|
|
if (__glibc_unlikely ((left | right) >= half_size_t))
|
|
{
|
|
if (__glibc_unlikely (right != 0 && *result / right != left))
|
|
return true;
|
|
}
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
#endif /* _MALLOC_INTERNAL_H */
|