netwerk/sctp/src/user_environment.c

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rwxr-xr-x

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /*-
michael@0 2 * Copyright (c) 2009-2010 Brad Penoff
michael@0 3 * Copyright (c) 2009-2010 Humaira Kamal
michael@0 4 * Copyright (c) 2011-2012 Irene Ruengeler
michael@0 5 * Copyright (c) 2011-2012 Michael Tuexen
michael@0 6 *
michael@0 7 * All rights reserved.
michael@0 8 *
michael@0 9 * Redistribution and use in source and binary forms, with or without
michael@0 10 * modification, are permitted provided that the following conditions
michael@0 11 * are met:
michael@0 12 * 1. Redistributions of source code must retain the above copyright
michael@0 13 * notice, this list of conditions and the following disclaimer.
michael@0 14 * 2. Redistributions in binary form must reproduce the above copyright
michael@0 15 * notice, this list of conditions and the following disclaimer in the
michael@0 16 * documentation and/or other materials provided with the distribution.
michael@0 17 *
michael@0 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
michael@0 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
michael@0 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
michael@0 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
michael@0 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
michael@0 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
michael@0 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
michael@0 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
michael@0 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
michael@0 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
michael@0 28 * SUCH DAMAGE.
michael@0 29 */
michael@0 30
michael@0 31 /* __Userspace__ */
michael@0 32
michael@0 33 #include <stdlib.h>
michael@0 34 #if !defined (__Userspace_os_Windows)
michael@0 35 #include <stdint.h>
michael@0 36 #include <netinet/sctp_os_userspace.h>
michael@0 37 #endif
michael@0 38 #include <user_environment.h>
michael@0 39 #include <sys/types.h>
michael@0 40 /* #include <sys/param.h> defines MIN */
michael@0 41 #if !defined(MIN)
michael@0 42 #define MIN(arg1,arg2) ((arg1) < (arg2) ? (arg1) : (arg2))
michael@0 43 #endif
michael@0 44 #include <string.h>
michael@0 45
michael@0 46 #define uHZ 1000
michael@0 47
michael@0 48 /* See user_include/user_environment.h for comments about these variables */
michael@0 49 int maxsockets = 25600;
michael@0 50 int hz = uHZ;
michael@0 51 int ip_defttl = 64;
michael@0 52 int ipport_firstauto = 49152, ipport_lastauto = 65535;
michael@0 53 int nmbclusters = 65536;
michael@0 54
michael@0 55 /* Source ip_output.c. extern'd in ip_var.h */
michael@0 56 u_short ip_id = 0; /*__Userspace__ TODO Should it be initialized to zero? */
michael@0 57
michael@0 58 /* used in user_include/user_atomic.h in order to make the operations
michael@0 59 * defined there truly atomic
michael@0 60 */
michael@0 61 userland_mutex_t atomic_mtx;
michael@0 62
michael@0 63 /* Source: /usr/src/sys/dev/random/harvest.c */
michael@0 64 static int read_random_phony(void *, int);
michael@0 65
michael@0 66 static int (*read_func)(void *, int) = read_random_phony;
michael@0 67
michael@0 68 /* Userland-visible version of read_random */
michael@0 69 int
michael@0 70 read_random(void *buf, int count)
michael@0 71 {
michael@0 72 return ((*read_func)(buf, count));
michael@0 73 }
michael@0 74
michael@0 75 /* If the entropy device is not loaded, make a token effort to
michael@0 76 * provide _some_ kind of randomness. This should only be used
michael@0 77 * inside other RNG's, like arc4random(9).
michael@0 78 */
michael@0 79 static int
michael@0 80 read_random_phony(void *buf, int count)
michael@0 81 {
michael@0 82 uint32_t randval;
michael@0 83 int size, i;
michael@0 84
michael@0 85 /* srandom() is called in kern/init_main.c:proc0_post() */
michael@0 86
michael@0 87 /* Fill buf[] with random(9) output */
michael@0 88 for (i = 0; i < count; i+= (int)sizeof(uint32_t)) {
michael@0 89 randval = random();
michael@0 90 size = MIN(count - i, (int)sizeof(uint32_t));
michael@0 91 memcpy(&((char *)buf)[i], &randval, (size_t)size);
michael@0 92 }
michael@0 93
michael@0 94 return (count);
michael@0 95 }
michael@0 96

mercurial