summaryrefslogtreecommitdiff
path: root/man
diff options
context:
space:
mode:
authorFaidon Liambotis <paravoid@debian.org>2020-12-31 12:46:03 +0200
committerGuillem Jover <guillem@hadrons.org>2021-01-01 18:18:51 +0100
commit01f0d1ea1e71f1018a009ebd9203dd48e6d90c45 (patch)
treea1dbb9416cea7186a8a5e6ee8c950c4f4683a94d /man
parent9c85d828a1abdd698f8a587abe8cbbaaa80eade8 (diff)
Add recallocarray() and freezero() from OpenBSD
Add recallocarray(), introduced in OpenBSD 6.1, and freezero(), introduced in OpenBSD 6.2. The former is imported as-is from OpenBSD, while the latter is the non-malloc-internal branch of the same code (and also the OpenSSH portable variant). Both of these originated in OpenBSD, but have also been implemented by IllumOS, cf. https://www.illumos.org/issues/8546 Documentation for these functions is in malloc(3) upstream, the relevant parts of which were previously imported in reallocarray(3bsd). Update reallocarray(3bsd) with the changes that were introduced since, and add the relevant bits for recallocarray() and freezero(), plus aliases. [guillem@hadrons.org: Update copyright in COPYING. ] Closes: !10 Signed-off-by: Guillem Jover <guillem@hadrons.org>
Diffstat (limited to 'man')
-rw-r--r--man/Makefile.am2
-rw-r--r--man/freezero.3bsd1
-rw-r--r--man/reallocarray.3bsd256
-rw-r--r--man/recallocarray.3bsd1
4 files changed, 226 insertions, 34 deletions
diff --git a/man/Makefile.am b/man/Makefile.am
index e3b27da..4c7ed05 100644
--- a/man/Makefile.am
+++ b/man/Makefile.am
@@ -165,6 +165,7 @@ dist_man_MANS = \
fgetln.3bsd \
fgetwln.3bsd \
flopen.3bsd \
+ freezero.3bsd \
fmtcheck.3bsd \
fparseln.3bsd \
fpurge.3bsd \
@@ -195,6 +196,7 @@ dist_man_MANS = \
readpassphrase.3bsd \
reallocarray.3bsd \
reallocf.3bsd \
+ recallocarray.3bsd \
setmode.3bsd \
setproctitle.3bsd \
setproctitle_init.3bsd \
diff --git a/man/freezero.3bsd b/man/freezero.3bsd
new file mode 100644
index 0000000..09471db
--- /dev/null
+++ b/man/freezero.3bsd
@@ -0,0 +1 @@
+.so man3/reallocarray.3bsd
diff --git a/man/reallocarray.3bsd b/man/reallocarray.3bsd
index 1a37d33..704466c 100644
--- a/man/reallocarray.3bsd
+++ b/man/reallocarray.3bsd
@@ -30,13 +30,15 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.\" $OpenBSD: malloc.3,v 1.78 2014/05/01 18:41:59 jmc Exp $
+.\" $OpenBSD: malloc.3,v 1.126 2019/09/14 13:16:50 otto Exp $
.\"
-.Dd $Mdocdate: May 1 2014 $
+.Dd $Mdocdate: September 14 2019 $
.Dt REALLOCARRAY 3bsd
.Os
.Sh NAME
-.Nm reallocarray
+.Nm reallocarray ,
+.Nm recallocarray ,
+.Nm freezero
.Nd memory allocation and deallocation
.Sh LIBRARY
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
@@ -49,59 +51,245 @@
for include usage.)
.Ft void *
.Fn reallocarray "void *ptr" "size_t nmemb" "size_t size"
+.Ft void *
+.Fn recallocarray "void *ptr" "size_t oldnmemb" "size_t nmemb" "size_t size"
+.Ft void
+.Fn freezero "void *ptr" "size_t size"
.Sh DESCRIPTION
.Pp
-When using
+Designed for safe allocation of arrays,
+the
+.Fn reallocarray
+function is similar to
+.Fn realloc
+except it operates on
+.Fa nmemb
+members of size
+.Fa size
+and checks for integer overflow in the calculation
+.Fa nmemb
+*
+.Fa size .
+.Pp
+Used for the allocation of memory holding sensitive data,
+the
+.Fn recallocarray
+function guarantees that memory becoming unallocated is explicitly
+.Em discarded ,
+meaning cached free objects are cleared with
+.Xr explicit_bzero 3 .
+.Pp
+The
+.Fn recallocarray
+function is similar to
+.Fn reallocarray
+except it ensures newly allocated memory is cleared similar to
+.Fn calloc .
+If
+.Fa ptr
+is
+.Dv NULL ,
+.Fa oldnmemb
+is ignored and the call is equivalent to
+.Fn calloc .
+If
+.Fa ptr
+is not
+.Dv NULL ,
+.Fa oldnmemb
+must be a value such that
+.Fa oldnmemb
+*
+.Fa size
+is the size of the earlier allocation that returned
+.Fa ptr ,
+otherwise the behavior is undefined.
+The
+.Fn freezero
+function is similar to the
+.Fn free
+function except it ensures memory is explicitly discarded.
+If
+.Fa ptr
+is
+.Dv NULL ,
+no action occurs.
+If
+.Fa ptr
+is not
+.Dv NULL ,
+the
+.Fa size
+argument must be equal to or smaller than the size of the earlier allocation
+that returned
+.Fa ptr .
+.Fn freezero
+guarantees the memory range starting at
+.Fa ptr
+with length
+.Fa size
+is discarded while deallocating the whole object originally allocated.
+.Sh RETURN VALUES
+The
+.Fn reallocarray
+and
+.Fn recallocarray
+functions return a pointer to the allocated space if successful; otherwise,
+a null pointer is returned and
+.Va errno
+is set to
+.Er ENOMEM .
+.Pp
+If multiplying
+.Fa nmemb
+and
+.Fa size
+results in integer overflow,
+.Fn reallocarray
+and
+.Fn recallocarray
+return
+.Dv NULL
+and set
+.Va errno
+to
+.Er ENOMEM .
+.Pp
+If
+.Fa ptr
+is not
+.Dv NULL
+and multiplying
+.Fa oldnmemb
+and
+.Fa size
+results in integer overflow
+.Fn recallocarray
+returns
+.Dv NULL
+and sets
+.Va errno
+to
+.Er EINVAL .
+.Sh IDIOMS
+Consider
+.Fn calloc
+or the extensions
+.Fn reallocarray
+and
+.Fn recallocarray
+when there is multiplication in the
+.Fa size
+argument of
.Fn malloc
-be careful to avoid the following idiom:
+or
+.Fn realloc .
+For example, avoid this common idiom as it may lead to integer overflow:
.Bd -literal -offset indent
if ((p = malloc(num * size)) == NULL)
- err(1, "malloc");
+ err(1, NULL);
.Ed
.Pp
-The multiplication may lead to an integer overflow, which can
-be avoided using the extension
-.Fn reallocarray ,
-as follows:
+A drop-in replacement is
+.Fn reallocarray :
.Bd -literal -offset indent
if ((p = reallocarray(NULL, num, size)) == NULL)
- err(1, "malloc");
+ err(1, NULL);
.Ed
.Pp
-Alternatively
+Alternatively,
.Fn calloc
-is a more portable solution which comes with the cost of clearing memory.
+may be used at the cost of initialization overhead.
.Pp
-If
-.Fn malloc
-must be used, be sure to test for overflow:
+When using
+.Fn realloc ,
+be careful to avoid the following idiom:
+.Bd -literal -offset indent
+size += 50;
+if ((p = realloc(p, size)) == NULL)
+ return (NULL);
+.Ed
+.Pp
+Do not adjust the variable describing how much memory has been allocated
+until the allocation has been successful.
+This can cause aberrant program behavior if the incorrect size value is used.
+In most cases, the above sample will also result in a leak of memory.
+As stated earlier, a return value of
+.Dv NULL
+indicates that the old object still remains allocated.
+Better code looks like this:
.Bd -literal -offset indent
-if (size && num > SIZE_MAX / size) {
- errno = ENOMEM;
- err(1, "overflow");
+newsize = size + 50;
+if ((newp = realloc(p, newsize)) == NULL) {
+ free(p);
+ p = NULL;
+ size = 0;
+ return (NULL);
}
+p = newp;
+size = newsize;
+.Ed
+.Pp
+As with
+.Fn malloc ,
+it is important to ensure the new size value will not overflow;
+i.e. avoid allocations like the following:
+.Bd -literal -offset indent
+if ((newp = realloc(p, num * size)) == NULL) {
+ ...
+.Ed
+.Pp
+Instead, use
+.Fn reallocarray :
+.Bd -literal -offset indent
+if ((newp = reallocarray(p, num, size)) == NULL) {
+ ...
+.Ed
+.Pp
+Calling
+.Fn realloc
+with a
+.Dv NULL
+.Fa ptr
+is equivalent to calling
+.Fn malloc .
+Instead of this idiom:
+.Bd -literal -offset indent
+if (p == NULL)
+ newp = malloc(newsize);
+else
+ newp = realloc(p, newsize);
+.Ed
+.Pp
+Use the following:
+.Bd -literal -offset indent
+newp = realloc(p, newsize);
.Ed
.Pp
-The use of
-.Fn reallocarray
-or
-.Fn calloc
-is strongly encouraged when allocating multiple sized objects
-in order to avoid possible integer overflows.
-.Sh RETURN VALUES
The
-.Fn reallocarray
-function returns a pointer to the allocated space if successful; otherwise,
-a null pointer is returned and
-.Va errno
-is set to
-.Er ENOMEM .
+.Fn recallocarray
+function should be used for resizing objects containing sensitive data like
+keys.
+To avoid leaking information,
+it guarantees memory is cleared before placing it on the internal free list.
+Deallocation of such an object should be done by calling
+.Fn freezero .
+
.Sh SEE ALSO
.Xr malloc 3 ,
.Xr calloc 3 ,
.Xr alloca 3
.Sh HISTORY
+The
.Fn reallocarray
-appeared in
+function appeared in
.Ox 5.6 ,
-glibc 2.26.
+and glibc 2.26.
+The
+.Fn recallocarray
+function appeared in
+.Ox 6.1 .
+The
+.Fn freezero
+function appeared in
+.Ox 6.2 .
diff --git a/man/recallocarray.3bsd b/man/recallocarray.3bsd
new file mode 100644
index 0000000..09471db
--- /dev/null
+++ b/man/recallocarray.3bsd
@@ -0,0 +1 @@
+.so man3/reallocarray.3bsd