1#!/bin/sh
2test "$1" = "-h" -o "$1" = "-hh" && {
3  echo 'afl-system-config by Marc Heuse <mh@mh-sec.de>'
4  echo
5  echo $0
6  echo
7  echo afl-system-config has no command line options
8  echo
9  echo afl-system reconfigures the system to a high performance fuzzing state
10  echo "WARNING: this reduces the security of the system!"
11  echo
12  exit 1
13}
14
15DONE=
16PLATFORM=`uname -s`
17echo This reconfigures the system to have a better fuzzing performance.
18echo "WARNING: this reduces the security of the system!"
19echo
20if [ '!' "$EUID" = 0 ] && [ '!' `id -u` = 0 ] ; then
21	echo "Warning: you need to be root to run this!"
22	# we do not exit as other mechanisms exist that allows to do this than
23	# being root. let the errors speak for themselves.
24fi
25sleep 1
26if [ "$PLATFORM" = "Linux" ] ; then
27{
28  sysctl -w kernel.core_uses_pid=0
29  # Arch Linux requires core_pattern to be empty :(
30  test -e /etc/arch-release && sysctl -w kernel.core_pattern=
31  test -e /etc/arch-release || sysctl -w kernel.core_pattern=core
32  sysctl -w kernel.randomize_va_space=0
33  sysctl -w kernel.sched_child_runs_first=1
34  sysctl -w kernel.sched_autogroup_enabled=1
35  sysctl -w kernel.sched_migration_cost_ns=50000000
36  sysctl -w kernel.sched_latency_ns=250000000
37  echo never > /sys/kernel/mm/transparent_hugepage/enabled
38  test -e /sys/devices/system/cpu/cpufreq/scaling_governor && echo performance | tee /sys/devices/system/cpu/cpufreq/scaling_governor
39  test -e /sys/devices/system/cpu/cpufreq/policy0/scaling_governor && echo performance | tee /sys/devices/system/cpu/cpufreq/policy*/scaling_governor
40  test -e /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor && echo performance | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
41  test -e /sys/devices/system/cpu/intel_pstate/no_turbo && echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo
42  test -e /sys/devices/system/cpu/cpufreq/boost && echo 1 > /sys/devices/system/cpu/cpufreq/boost
43  test -e /sys/devices/system/cpu/intel_pstate/max_perf_pct && echo 100 > /sys/devices/system/cpu/intel_pstate/max_perf_pct
44  test -n "$(which auditctl)" && auditctl -a never,task >/dev/null 2>&1
45} > /dev/null
46  echo Settings applied.
47  echo
48  dmesg | egrep -q 'nospectre_v2|spectre_v2=off' || {
49    echo It is recommended to boot the kernel with lots of security off - if you are running a machine that is in a secured network - so set this:
50    echo '  /etc/default/grub:GRUB_CMDLINE_LINUX_DEFAULT="ibpb=off ibrs=off kpti=0 l1tf=off mds=off mitigations=off no_stf_barrier noibpb noibrs nopcid nopti nospec_store_bypass_disable nospectre_v1 nospectre_v2 pcid=off pti=off spec_store_bypass_disable=off spectre_v2=off stf_barrier=off srbds=off noexec=off noexec32=off tsx=on tsx_async_abort=off arm64.nopauth audit=0 hardened_usercopy=off ssbd=force-off"'
51    echo
52  }
53  echo If you run fuzzing instances in docker, run them with \"--security-opt seccomp=unconfined\" for more speed
54  echo
55  DONE=1
56fi
57if [ "$PLATFORM" = "FreeBSD" ] ; then
58{
59  sysctl kern.elf32.aslr.enable=0
60  sysctl kern.elf64.aslr.enable=0
61} > /dev/null
62  echo Settings applied.
63  echo
64  cat <<EOF
65In order to suppress core file generation during fuzzing it is recommended to set
66me:\\
67	:coredumpsize=0:
68in the ~/.login_conf file for the user used for fuzzing.
69EOF
70  echo It is recommended to boot the kernel with lots of security off - if you are running a machine that is in a secured network - so set this:
71  echo '  sysctl hw.ibrs_disable=1'
72  echo 'Setting kern.pmap.pg_ps_enabled=0 into /boot/loader.conf might be helpful too.'
73  echo
74  DONE=1
75fi
76if [ "$PLATFORM" = "OpenBSD" ] ; then
77  echo 'System security features cannot be disabled on OpenBSD.'
78  echo
79  DONE=1
80fi
81if [ "$PLATFORM" = "DragonFly" ] ; then
82  #/sbin/sysctl kern.corefile=/dev/null
83  #echo Settings applied.
84  cat <<EOF
85In order to suppress core file generation during fuzzing it is recommended to set
86me:\\
87	:coredumpsize=0:
88in the ~/.login_conf file for the user used for fuzzing.
89EOF
90  echo
91  DONE=1
92fi
93if [ "$PLATFORM" = "NetBSD" ] ; then
94{
95  /sbin/sysctl -w security.models.extensions.user_set_cpu_affinity=1
96} > /dev/null
97  echo Settings applied.
98  echo
99  DONE=1
100fi
101if [ "$PLATFORM" = "Darwin" ] ; then
102  sysctl kern.sysv.shmmax=8388608
103  sysctl kern.sysv.shmseg=48
104  sysctl kern.sysv.shmall=98304
105  echo Settings applied.
106  echo
107  if [ $(launchctl list 2>/dev/null | grep -q '\.ReportCrash$') ] ; then
108    echo
109    echo Unloading the default crash reporter
110    SL=/System/Library; PL=com.apple.ReportCrash
111    launchctl unload -w ${SL}/LaunchAgents/${PL}.plist >/dev/null 2>&1
112    sudo launchctl unload -w ${SL}/LaunchDaemons/${PL}.Root.plist >/dev/null 2>&1
113    echo
114  fi
115  echo It is recommended to disable System Integration Protection for increased performance.
116  echo
117  DONE=1
118fi
119if [ "$PLATFORM" = "Haiku" ] ; then
120  DEBUG_SERVER_DIR=~/config/settings/system/debug_server
121  [ ! -d ${DEBUG_SERVER_DIR} ] && mkdir -p ${DEBUG_SERVER_DIR}
122  SETTINGS=${DEBUG_SERVER_DIR}/settings
123  [ -r ${SETTINGS} ] && grep -qE "default_action\s+kill" ${SETTINGS} && { echo "Nothing to do"; } || { \
124    echo We change the debug_server default_action from user to silently kill; \
125    [ ! -r ${SETTINGS} ] && echo "default_action kill" >${SETTINGS} || { mv ${SETTINGS} s.tmp; sed -e "s/default_action\s\s*user/default_action kill/" s.tmp > ${SETTINGS}; rm s.tmp; }; \
126    echo Settings applied.; echo; \
127  }
128  DONE=1
129fi
130test -z "$DONE" && echo Error: Unknown platform: $PLATFORM
131exit 0
132