From owner-svn-src-all@FreeBSD.ORG Sat Mar 28 04:00:47 2009 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 1C314106564A; Sat, 28 Mar 2009 04:00:47 +0000 (UTC) (envelope-from delphij@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 09D318FC1A; Sat, 28 Mar 2009 04:00:47 +0000 (UTC) (envelope-from delphij@FreeBSD.org) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id n2S40kuk083705; Sat, 28 Mar 2009 04:00:46 GMT (envelope-from delphij@svn.freebsd.org) Received: (from delphij@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id n2S40kW1083700; Sat, 28 Mar 2009 04:00:46 GMT (envelope-from delphij@svn.freebsd.org) Message-Id: <200903280400.n2S40kW1083700@svn.freebsd.org> From: Xin LI Date: Sat, 28 Mar 2009 04:00:46 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r190482 - in head/lib/libc/db: . btree hash mpool X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 28 Mar 2009 04:00:47 -0000 Author: delphij Date: Sat Mar 28 04:00:46 2009 New Revision: 190482 URL: http://svn.freebsd.org/changeset/base/190482 Log: When allocating memory, zero out them if we don't intend to overwrite them all; before freeing memory, zero out them before we release it as free heap. This will eliminate some potential information leak issue. While there, remove the PURIFY option. There is a slight difference between the new behavior and the old -DPURIFY behavior, with the latter initializes memory with 0xff's. The difference between old and new approach does not generate observable difference. Obtained from: OpenBSD (partly). Modified: head/lib/libc/db/README head/lib/libc/db/btree/bt_open.c head/lib/libc/db/btree/bt_split.c head/lib/libc/db/hash/hash_buf.c head/lib/libc/db/mpool/mpool.c Modified: head/lib/libc/db/README ============================================================================== --- head/lib/libc/db/README Fri Mar 27 21:47:56 2009 (r190481) +++ head/lib/libc/db/README Sat Mar 28 04:00:46 2009 (r190482) @@ -1,4 +1,5 @@ # @(#)README 8.27 (Berkeley) 9/1/94 +# $FreeBSD$ This is version 1.85 of the Berkeley DB code. @@ -31,10 +32,3 @@ mpool The memory pool routines. recno The fixed/variable length record routines. test Test package. -============================================ -Debugging: - -If you're running a memory checker (e.g. Purify) on DB, make sure that -you recompile it with "-DPURIFY" in the CFLAGS, first. By default, -allocated pages are not initialized by the DB code, and they will show -up as reads of uninitialized memory in the buffer write routines. Modified: head/lib/libc/db/btree/bt_open.c ============================================================================== --- head/lib/libc/db/btree/bt_open.c Fri Mar 27 21:47:56 2009 (r190481) +++ head/lib/libc/db/btree/bt_open.c Sat Mar 28 04:00:46 2009 (r190482) @@ -156,9 +156,8 @@ __bt_open(const char *fname, int flags, goto einval; /* Allocate and initialize DB and BTREE structures. */ - if ((t = (BTREE *)malloc(sizeof(BTREE))) == NULL) + if ((t = (BTREE *)calloc(1, sizeof(BTREE))) == NULL) goto err; - memset(t, 0, sizeof(BTREE)); t->bt_fd = -1; /* Don't close unopened fd on error. */ t->bt_lorder = b.lorder; t->bt_order = NOT; @@ -166,9 +165,8 @@ __bt_open(const char *fname, int flags, t->bt_pfx = b.prefix; t->bt_rfd = -1; - if ((t->bt_dbp = dbp = (DB *)malloc(sizeof(DB))) == NULL) + if ((t->bt_dbp = dbp = (DB *)calloc(1, sizeof(DB))) == NULL) goto err; - memset(t->bt_dbp, 0, sizeof(DB)); if (t->bt_lorder != machine_lorder) F_SET(t, B_NEEDSWAP); Modified: head/lib/libc/db/btree/bt_split.c ============================================================================== --- head/lib/libc/db/btree/bt_split.c Fri Mar 27 21:47:56 2009 (r190481) +++ head/lib/libc/db/btree/bt_split.c Sat Mar 28 04:00:46 2009 (r190482) @@ -372,13 +372,10 @@ bt_page(BTREE *t, PAGE *h, PAGE **lp, PA } /* Put the new left page for the split into place. */ - if ((l = (PAGE *)malloc(t->bt_psize)) == NULL) { + if ((l = (PAGE *)calloc(1, t->bt_psize)) == NULL) { mpool_put(t->bt_mp, r, 0); return (NULL); } -#ifdef PURIFY - memset(l, 0xff, t->bt_psize); -#endif l->pgno = h->pgno; l->nextpg = r->pgno; l->prevpg = h->prevpg; Modified: head/lib/libc/db/hash/hash_buf.c ============================================================================== --- head/lib/libc/db/hash/hash_buf.c Fri Mar 27 21:47:56 2009 (r190481) +++ head/lib/libc/db/hash/hash_buf.c Sat Mar 28 04:00:46 2009 (r190482) @@ -57,6 +57,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #ifdef DEBUG #include @@ -169,18 +170,12 @@ newbuf(HTAB *hashp, u_int32_t addr, BUFH */ if (hashp->nbufs || (bp->flags & BUF_PIN)) { /* Allocate a new one */ - if ((bp = (BUFHEAD *)malloc(sizeof(BUFHEAD))) == NULL) + if ((bp = (BUFHEAD *)calloc(1, sizeof(BUFHEAD))) == NULL) return (NULL); -#ifdef PURIFY - memset(bp, 0xff, sizeof(BUFHEAD)); -#endif - if ((bp->page = (char *)malloc(hashp->BSIZE)) == NULL) { + if ((bp->page = (char *)calloc(1, hashp->BSIZE)) == NULL) { free(bp); return (NULL); } -#ifdef PURIFY - memset(bp->page, 0xff, hashp->BSIZE); -#endif if (hashp->nbufs) hashp->nbufs--; } else { @@ -319,8 +314,10 @@ __buf_free(HTAB *hashp, int do_free, int } /* Check if we are freeing stuff */ if (do_free) { - if (bp->page) + if (bp->page) { + (void)memset(bp->page, 0, hashp->BSIZE); free(bp->page); + } BUF_REMOVE(bp); free(bp); bp = LRU; Modified: head/lib/libc/db/mpool/mpool.c ============================================================================== --- head/lib/libc/db/mpool/mpool.c Fri Mar 27 21:47:56 2009 (r190481) +++ head/lib/libc/db/mpool/mpool.c Sat Mar 28 04:00:46 2009 (r190482) @@ -332,14 +332,11 @@ mpool_bkt(MPOOL *mp) return (bp); } -new: if ((bp = (BKT *)malloc(sizeof(BKT) + mp->pagesize)) == NULL) +new: if ((bp = (BKT *)calloc(1, sizeof(BKT) + mp->pagesize)) == NULL) return (NULL); #ifdef STATISTICS ++mp->pagealloc; #endif -#if defined(DEBUG) || defined(PURIFY) - memset(bp, 0xff, sizeof(BKT) + mp->pagesize); -#endif bp->page = (char *)bp + sizeof(BKT); ++mp->curcache; return (bp);