xref: /xnu-8020.140.41/san/tools/kasan_install (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1#!/bin/bash
2
3#
4# kasan_install: set up a system to run the KASan kernel. Run with "--uninstall"
5# to reverse the setup.
6#
7# Adds kcsuffix=kasan to boot-args.
8#
9
10
11if [[ `whoami` != root ]] ; then
12	echo "Re-running with sudo"
13	sudo "$0" "$@"
14	exit $?
15fi
16
17sip_enabled() {
18	csrutil status |grep -q enabled
19}
20
21prompt() {
22	echo -n "$@ [y/N] "
23	read ans
24	case "$ans" in
25		[yY]*) return 0 ;;
26		*) return 1 ;;
27	esac
28}
29
30kasan_install() {
31
32	dobootargs=0
33
34	echo -n "Checking KASan boot args... "
35
36	bootargs=$(nvram boot-args | cut -f2)
37	cursuffix=$(echo $bootargs | sed -n 's/.*kcsuffix=\([^ ]\)/\1/p')
38
39	if [[ "$cursuffix" == kasan ]] ; then
40		echo "already set."
41	elif [[ -n "$cursuffix" ]] ; then
42		prompt "custom kcsuffix ($cursuffix) is set. Overwrite?" && {
43			bootargs=$(echo "$bootargs" | sed 's/[ ]*kcsuffix=[^ ]*//')
44			dobootargs=1
45		}
46	else
47		prompt "not set. Modify?" && {
48			dobootargs=1
49		}
50	fi
51
52	[[ $dobootargs -eq 1 ]] && {
53		echo -n "Adding boot arg kcsuffix=kasan... "
54		newlen=$(echo -n "$bootargs kcsuffix=kasan" |wc -c)
55		if [[ $newlen -ge 512 ]] ; then
56			echo "boot-args too long. Bailing."
57			exit 3
58		fi
59
60		nvram boot-args="$bootargs kcsuffix=kasan" || exit $?
61		echo "done."
62	}
63
64}
65
66
67kasan_uninstall() {
68
69	echo -n "Removing boot args... "
70
71	bootargs=$(nvram boot-args | cut -f2)
72	cursuffix=$(echo $bootargs | sed -n 's/.*kcsuffix=\([^ ]\)/\1/p')
73
74	if [[ $cursuffix == "kasan" ]] ; then
75		prompt "remove kcsuffix=kasan?" && {
76			echo -n "Removing kcsuffix... "
77			bootargs=$(echo "$bootargs" | sed 's/[ ]*kcsuffix=[^ ]*//')
78			nvram boot-args="$bootargs"
79			echo "done."
80		}
81	else
82		echo "not set."
83	fi
84
85}
86
87case "$1" in
88	*uninstall|*del*|*remove|*rm)
89		kasan_uninstall ;;
90	*)
91		kasan_install ;;
92esac
93