From: Jakub Jelinek <jakub@redhat.com>
To: Ulrich Drepper <drepper@redhat.com>
Cc: Glibc hackers <libc-hacker@sources.redhat.com>
Subject: [PATCH] xdr_array and calloc security fix
Date: Thu, 01 Aug 2002 15:46:00 -0000 [thread overview]
Message-ID: <20020802004635.Y20867@sunsite.ms.mff.cuni.cz> (raw)
Hi!
2002-08-02 Jakub Jelinek <jakub@redhat.com>
* malloc/malloc.c (public_cALLOc): Check for overflow on
multiplication.
* sunrpc/xdr_array.c (xdr_array): Likewise.
* sunrpc/rpc/types.h (mem_free): Add comment.
Patch by Solar Designer <solar@openwall.com>.
--- libc/malloc/malloc.c.jj 2002-06-21 11:37:55.000000000 +0200
+++ libc/malloc/malloc.c 2002-08-02 00:58:16.000000000 +0200
@@ -3452,16 +3452,23 @@ public_cALLOc(size_t n, size_t elem_size
{
mstate av;
mchunkptr oldtop, p;
- INTERNAL_SIZE_T sz, csz, oldtopsize;
+ INTERNAL_SIZE_T bytes, sz, csz, oldtopsize;
Void_t* mem;
unsigned long clearsize;
unsigned long nclears;
INTERNAL_SIZE_T* d;
-
__malloc_ptr_t (*hook) __MALLOC_PMT ((size_t, __const __malloc_ptr_t)) =
__malloc_hook;
+
+ /* size_t is unsigned so the behavior on overflow is defined. */
+ bytes = n * elem_size;
+ if (bytes / elem_size != n) {
+ MALLOC_FAILURE_ACTION;
+ return 0;
+ }
+
if (hook != NULL) {
- sz = n * elem_size;
+ sz = bytes;
mem = (*hook)(sz, RETURN_ADDRESS (0));
if(mem == 0)
return 0;
@@ -3473,8 +3480,7 @@ public_cALLOc(size_t n, size_t elem_size
#endif
}
- /* FIXME: check for overflow on multiplication. */
- sz = n * elem_size;
+ sz = bytes;
arena_get(av, sz);
if(!av)
--- libc/sunrpc/rpc/types.h.jj 2001-06-25 10:34:46.000000000 +0200
+++ libc/sunrpc/rpc/types.h 2002-08-02 00:59:16.000000000 +0200
@@ -58,6 +58,10 @@ typedef unsigned long rpcport_t;
#include <stdlib.h> /* For malloc decl. */
#define mem_alloc(bsize) malloc(bsize)
+/*
+ * XXX: This must not use the second argument, or code in xdr_array.c needs
+ * to be modified.
+ */
#define mem_free(ptr, bsize) free(ptr)
#ifndef makedev /* ie, we haven't already included it */
--- libc/sunrpc/xdr_array.c.jj 2002-02-28 12:32:13.000000000 +0100
+++ libc/sunrpc/xdr_array.c 2002-08-02 01:00:39.000000000 +0200
@@ -45,6 +45,7 @@ static char sccsid[] = "@(#)xdr_array.c
#include <rpc/types.h>
#include <rpc/xdr.h>
#include <libintl.h>
+#include <limits.h>
#ifdef USE_IN_LIBIO
# include <wchar.h>
@@ -81,7 +82,11 @@ xdr_array (xdrs, addrp, sizep, maxsize,
return FALSE;
}
c = *sizep;
- if ((c > maxsize) && (xdrs->x_op != XDR_FREE))
+ /*
+ * XXX: Let the overflow possibly happen with XDR_FREE because mem_free()
+ * doesn't actually use its second argument anyway.
+ */
+ if ((c > maxsize || c > UINT_MAX / elsize) && (xdrs->x_op != XDR_FREE))
{
return FALSE;
}
Jakub
next reply other threads:[~2002-08-01 22:46 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-08-01 15:46 Jakub Jelinek [this message]
2002-08-02 0:20 ` Ulrich Drepper
2002-08-02 2:29 ` Wolfram Gloger
2002-08-02 2:43 ` Ulrich Drepper
2002-08-02 2:50 ` Ulrich Drepper
2002-08-02 2:56 ` Jakub Jelinek
2002-08-02 4:07 ` Andreas Schwab
2002-08-02 4:46 ` Jakub Jelinek
2002-08-02 4:57 ` Wolfram Gloger
2002-08-02 5:05 ` Jakub Jelinek
2002-08-02 5:13 ` Wolfram Gloger
2002-08-03 5:03 ` [PATCH] optimize calloc Jakub Jelinek
2002-08-03 12:02 ` Ulrich Drepper
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20020802004635.Y20867@sunsite.ms.mff.cuni.cz \
--to=jakub@redhat.com \
--cc=drepper@redhat.com \
--cc=libc-hacker@sources.redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).