strace: generic "when:" support?

Steffen Nurpmeso steffen at sdaoden.eu
Thu Apr 13 20:58:23 UTC 2023


Hello!

First of all a "Thank you!" for this very helpful tool, which, as
i learned, exists for many decades, and is still actively
maintained!  So thank you for this.

Now i am, de facto, not a frequent, let alone a power user of it.
Nonetheless.. :)

Well my "problem" was that while rewriting a small client/server
thing for security sandboxing on Linux (and FreeBSD) i wanted to
learn more easily which syscalls, effectively, are in use when
running the program, and strace really does this, including fork
following and such, that is just _wonderful_.  I could write
a make rule which runs the program, let strace trace, and
automatically extract rules (let me paste the rule after this
text, just for fun).

However, and here my wish / suggestion.  I had seen on i think the
nmh (MUA) mailing-list a code snippet with strace that used
a "when:" clause.  (Or was it TUHS?  Ralph Corderoy was the
author, that much i remember for sure.)

Unfortunately strace does not support a "when:" clause regulary,
so that the listings generated by the make rule cover the entire
lifetime of the program, not only the sandboxed parts.
Wouldn't this be great if the log creation could be controlled via
a when-cause generally?

Thank you anyway, and i say Ciao! from Germany already now.

P.S.: (that should have been placed in a sh(1)ell script instead.)

# test-strace {{{
test-strace: all
	if [ "$(VAL_OS_SANDBOX)" -ne 0 ]; then echo >&2 this will not do; exit 1; fi;\
	trap "rm -rf .z .b.rc .r.rc .c.xout .c.out .s.strace .c.strace" EXIT; trap "exit 1" INT HUP QUIT TERM;\
	mkdir .z || exit 2;\
	{ \
		echo action=DEFER_IF_PERMIT 4.2.0;echo;\
		echo action=DUNNO;echo;\
		echo action=REJECT;echo;\
		echo action=DUNNO;echo;\
		echo action=DUNNO;echo;\
		echo action=REJECT;echo;\
	} > .c.xout || exit 3;\
	echo test.localdomain > .b.rc || exit 4;\
	echo test2.localdomain > .z/a.rc || exit 5;\
	pwd=$$(pwd);\
	{ \
		echo msg-defer DEFER_IF_PERMIT 4.2.0;\
		echo store-path $$pwd/.z; echo block-file $$pwd/.b.rc; echo allow-file $$pwd/.z/a.rc;\
		echo verbose; echo verbose; echo count 1; echo delay-min 0;\
	} > .r.rc || exit 6;\
	\
	strace -f -c -U name -o .s.strace ./"$(VAL_NAME)" -R $$pwd/.r.rc --startup & [ $$? -eq 0 ] || exit 10;\
	sleep 2;\
	{ \
	echo recipient=x1 at y; echo sender=y at z; echo client_address=127.1.2.2; echo client_name=xy; echo;\
	echo recipient=x1 at y; echo sender=y at z; echo client_address=127.1.2.2; echo client_name=test2.localdomain; echo;\
	echo recipient=x1 at y; echo sender=y at z; echo client_address=127.1.2.2; echo client_name=test.localdomain; echo;\
	echo recipient=x1 at y; echo sender=y at z; echo client_address=127.1.2.2; echo client_name=xy; echo;\
	} | strace -c -U name -o .c.strace ./"$(VAL_NAME)" -R $$pwd/.r.rc >> .c.out || exit 11;\
	sleep 2;\
	\
	./"$(VAL_NAME)" -R $$pwd/.r.rc --status || exit 12;\
	./"$(VAL_NAME)" -R $$pwd/.r.rc --shutdown || exit 13;\
	./"$(VAL_NAME)" -R $$pwd/.r.rc --status && exit 14;\
	\
	echo once >> .r.rc || exit 20;\
	strace -A -f -c -U name -o .s.strace ./"$(VAL_NAME)" -R $$pwd/.r.rc --startup & [ $$? -eq 0 ] || exit 21;\
	{ \
	echo recipient=x1 at y; echo sender=y at z; echo client_address=127.1.2.2; echo client_name=xy; echo;\
	echo this should not create result;echo;\
	} | strace -A -c -U name -o .c.strace ./"$(VAL_NAME)" -R $$pwd/.r.rc >> .c.out || exit 22;\
	sleep 2;\
	./"$(VAL_NAME)" -R $$pwd/.r.rc --status || exit 23;\
	\
	echo 'block xy' >> .r.rc || exit 24;\
	kill -HUP $$(cat $$pwd/.z/"$(VAL_NAME)".pid) || exit 25;\
	sleep 2;\
	kill -USR1 $$(cat $$pwd/.z/"$(VAL_NAME)".pid) || exit 26;\
	sleep 2;\
	kill -USR2 $$(cat $$pwd/.z/"$(VAL_NAME)".pid) || exit 27;\
	sleep 2;\
	{ \
	echo recipient=x1 at y; echo sender=y at z; echo client_address=127.1.2.2; echo client_name=xy; echo;\
	} | strace -A -c -U name -o .c.strace ./"$(VAL_NAME)" -R $$pwd/.r.rc >> .c.out || exit 28;\
	\
	./"$(VAL_NAME)" -R $$pwd/.r.rc --status || exit 29;\
	./"$(VAL_NAME)" -R $$pwd/.r.rc --shutdown || exit 30;\
	\
	diff -u .c.xout .c.out; echo diff said $$?;\
	\
	< .c.strace awk '\
		BEGIN{c=hot=0}\
		/^-+$$/{hot=!hot;next}\
		{if(!hot) next; for(i=1; i <= c; ++i) if(a[i] == $$1) next; a[++c] = $$1}\
		END{for(i=1;i<=c;++i) print "a_Y(SYS_" a[i] "),"}\
	' > .c.txt;\
	echo 'VAL_OS_SANDBOX_CLIENT_RULES="'$$(cat .c.txt)'"';\
	\
	< .s.strace awk '\
		BEGIN{c=hot=0}\
		/^-+$$/{hot=!hot;next}\
		{if(!hot) next; for(i=1; i <= c; ++i) if(a[i] == $$1) next; a[++c] = $$1}\
		END{for(i=1;i<=c;++i) print "a_Y(SYS_" a[i] "),"}\
	' > .s.txt;\
	echo 'VAL_OS_SANDBOX_SERVER_RULES="'$$(cat .s.txt)'"';
# }}}

--steffen
|
|Der Kragenbaer,                The moon bear,
|der holt sich munter           he cheerfully and one by one
|einen nach dem anderen runter  wa.ks himself off
|(By Robert Gernhardt)


More information about the Strace-devel mailing list