From owner-svn-src-head@freebsd.org Tue Jan 9 06:44:00 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 30C2EE66FA8; Tue, 9 Jan 2018 06:44:00 +0000 (UTC) (envelope-from cy@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 0B63E6CB20; Tue, 9 Jan 2018 06:44:00 +0000 (UTC) (envelope-from cy@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 3280913627; Tue, 9 Jan 2018 06:43:59 +0000 (UTC) (envelope-from cy@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w096hxRV037347; Tue, 9 Jan 2018 06:43:59 GMT (envelope-from cy@FreeBSD.org) Received: (from cy@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w096hxBt037346; Tue, 9 Jan 2018 06:43:59 GMT (envelope-from cy@FreeBSD.org) Message-Id: <201801090643.w096hxBt037346@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: cy set sender to cy@FreeBSD.org using -f From: Cy Schubert Date: Tue, 9 Jan 2018 06:43:59 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r327718 - head/sys/contrib/ipfilter/netinet X-SVN-Group: head X-SVN-Commit-Author: cy X-SVN-Commit-Paths: head/sys/contrib/ipfilter/netinet X-SVN-Commit-Revision: 327718 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 09 Jan 2018 06:44:00 -0000 Author: cy Date: Tue Jan 9 06:43:58 2018 New Revision: 327718 URL: https://svnweb.freebsd.org/changeset/base/327718 Log: When growing the state, also grow the seed array. Otherwise memory that was not allocated will be accessed. This necessitated refactoring state seed allocation from ipf_state_soft_init() into a new common ipf_state_seed_alloc() function as it is now also used by ipf_state_rehash() when changing the size of the state hash table in addition to by ipf_state_soft_init() during initialization. According to Christos Zoulas : The bug was encountered by a NetBSD vendor who's customer machines had large ipfilter states. The bug was reliably triggered by resizing the state variables using "ipf -T". Submitted by: Christos Zoulas Reviewed by: delphij, rgrimes Obtained from: NetBSD ip_state.c CVS revs r1.9 and r1.10 MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D13755 Modified: head/sys/contrib/ipfilter/netinet/ip_state.c Modified: head/sys/contrib/ipfilter/netinet/ip_state.c ============================================================================== --- head/sys/contrib/ipfilter/netinet/ip_state.c Tue Jan 9 06:10:57 2018 (r327717) +++ head/sys/contrib/ipfilter/netinet/ip_state.c Tue Jan 9 06:43:58 2018 (r327718) @@ -301,7 +301,33 @@ ipf_state_soft_destroy(softc, arg) KFREE(softs); } +static void * +ipf_state_seed_alloc(u_int state_size, u_int state_max) +{ + u_int i; + u_long *state_seed; + KMALLOCS(state_seed, u_long *, state_size * sizeof(*state_seed)); + if (state_seed == NULL) + return NULL; + for (i = 0; i < state_size; i++) { + /* + * XXX - ipf_state_seed[X] should be a random number of sorts. + */ +#if FREEBSD_GE_REV(400000) + state_seed[i] = arc4random(); +#else + state_seed[i] = ((u_long)state_seed + i) * state_size; + state_seed[i] ^= 0xa5a55a5a; + state_seed[i] *= (u_long)state_seed; + state_seed[i] ^= 0x5a5aa5a5; + state_seed[i] *= state_max; +#endif + } + return state_seed; +} + + /* ------------------------------------------------------------------------ */ /* Function: ipf_state_soft_init */ /* Returns: int - 0 == success, -1 == failure */ @@ -333,27 +359,11 @@ ipf_state_soft_init(softc, arg) bzero((char *)softs->ipf_state_table, softs->ipf_state_size * sizeof(ipstate_t *)); - KMALLOCS(softs->ipf_state_seed, u_long *, - softs->ipf_state_size * sizeof(*softs->ipf_state_seed)); + softs->ipf_state_seed = ipf_state_seed_alloc(softs->ipf_state_size, + softs->ipf_state_max); if (softs->ipf_state_seed == NULL) return -2; - for (i = 0; i < softs->ipf_state_size; i++) { - /* - * XXX - ipf_state_seed[X] should be a random number of sorts. - */ -#if FREEBSD_GE_REV(400000) - softs->ipf_state_seed[i] = arc4random(); -#else - softs->ipf_state_seed[i] = ((u_long)softs->ipf_state_seed + i) * - softs->ipf_state_size; - softs->ipf_state_seed[i] ^= 0xa5a55a5a; - softs->ipf_state_seed[i] *= (u_long)softs->ipf_state_seed; - softs->ipf_state_seed[i] ^= 0x5a5aa5a5; - softs->ipf_state_seed[i] *= softs->ipf_state_max; -#endif - } - KMALLOCS(softs->ipf_state_stats.iss_bucketlen, u_int *, softs->ipf_state_size * sizeof(u_int)); if (softs->ipf_state_stats.iss_bucketlen == NULL) @@ -5259,6 +5269,7 @@ ipf_state_rehash(softc, t, p) { ipf_state_softc_t *softs = softc->ipf_state_soft; ipstate_t **newtab, *is; + u_long *newseed; u_int *bucketlens; u_int maxbucket; u_int newsize; @@ -5285,6 +5296,14 @@ ipf_state_rehash(softc, t, p) return ENOMEM; } + newseed = ipf_state_seed_alloc(newsize, softs->ipf_state_max); + if (newseed == NULL) { + KFREES(bucketlens, newsize * sizeof(*bucketlens)); + KFREES(newtab, newsize * sizeof(*newtab)); + IPFERROR(100037); + return ENOMEM; + } + for (maxbucket = 0, i = newsize; i > 0; i >>= 1) maxbucket++; maxbucket *= 2; @@ -5299,6 +5318,12 @@ ipf_state_rehash(softc, t, p) softs->ipf_state_size * sizeof(*softs->ipf_state_table)); } softs->ipf_state_table = newtab; + + if (softs->ipf_state_seed != NULL) { + KFREES(softs->ipf_state_seed, + softs->ipf_state_size * sizeof(*softs->ipf_state_seed)); + } + softs->ipf_state_seed = newseed; if (softs->ipf_state_stats.iss_bucketlen != NULL) { KFREES(softs->ipf_state_stats.iss_bucketlen,