MAKEFLAGS += --no-print-directory

KYRG_ROOT := /sys/kernel/security/kyrg/


build: build-proess build-module

clean:
	make -f Kbuild clean
	rm -f *.o attack_victim

test: test-process-attack test-module-attack

build-proess: attack_victim

build-module:
	make -f Kbuild build

test-module-attack: _restore-module-attack
	# 1. run
	insmod hello01.ko
	# 2. add policy 
	echo "add hello01 0" | cat > $(KYRG_ROOT)/policy_modules
	# 3. attack to modified hello01
	insmod attackmod.ko
	# 4. triger the protection
	echo 1 | cat > $(KYRG_ROOT)/trigger
	# 5. check the result

_restore-module-attack:
	@echo "del hello01" | cat > $(KYRG_ROOT)/policy_modules
	@lsmod | grep attackmod && rmmod attackmod || true
	@lsmod | grep hello01 && rmmod hello01 || true

test-process-attack: _restore-process-attack
	# 1. add policy
	echo "add $(realpath ./attack_victim) 0" | cat > $(KYRG_ROOT)/policy_processes
	# 2. run 
	./attack_victim > /dev/null &
	# 3. attack the process 
	./attack_ptrace
	# 4. triger the protection
	echo 1 | cat > $(KYRG_ROOT)/trigger
	# 5. check the result

_restore-process-attack:
	@echo "del $(realpath ./attack_victim)" | cat > $(KYRG_ROOT)/policy_processes
	@pidof attack_victim && kill -9 $$(pidof attack_victim) || true

