Skip to contentSkip to conversation

fs/binfmt_misc01.sh: Add new test for invalid inputs

PATCH New Xiao Yang 2 participants 5 messages Feb 18, 2019 – Feb 21, 2019 exec and binfmt

lore
Share & tools
lore

Xiao Yang

author
#0 lore raw

PATCH 1/1 fs/binfmt_misc01.sh: Add new test for invalid inputs

Use various invalid inputs to register a new binary type.

Note:
This is also a regression test for the following kernel bug:
'5cc41e099504 ("fs/binfmt_misc.c: do not allow offset overflow")

---
 runtest/fs                                         |  2 +
 testcases/kernel/fs/binfmt_misc/Makefile           | 12 ++++
 testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh   | 65 ++++++++++++++++++++++
 testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh | 64 +++++++++++++++++++++
 4 files changed, 143 insertions(+)
 create mode 100644 testcases/kernel/fs/binfmt_misc/Makefile
 create mode 100755 testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh
 create mode 100755 testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh
runtest/fs +2 -0
index aca7e35..227c186 100644
@@ -82,3 +82,5 @@ quota_remount_test01 quota_remount_test01.sh
 isofs isofs.sh
 
 fs_fill fs_fill
+
+binfmt_misc01 binfmt_misc01.sh
testcases/kernel/fs/binfmt_misc/Makefile new file +12 -0
new file mode 100644
index 0000000..f9819ad
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
+# Author: Xiao Yang <[email protected]>
+
+top_srcdir              ?= ../../../..
+
+include $(top_srcdir)/include/mk/env_pre.mk
+
+INSTALL_TARGETS         := binfmt_misc01.sh binfmt_misc_lib.sh
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh new file +65 -0

65 lines not on this page · raw message

testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh new file +64 -0

64 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/1 fs/binfmt_misc01.sh: Add new test for invalid inputs

Hi!

Quoted 61 lines
---
 runtest/fs                                         |  2 +
 testcases/kernel/fs/binfmt_misc/Makefile           | 12 ++++
 testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh   | 65 ++++++++++++++++++++++
 testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh | 64 +++++++++++++++++++++
 4 files changed, 143 insertions(+)
 create mode 100644 testcases/kernel/fs/binfmt_misc/Makefile
 create mode 100755 testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh
 create mode 100755 testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh
runtest/fs +2 -0
index aca7e35..227c186 100644
@@ -82,3 +82,5 @@ quota_remount_test01 quota_remount_test01.sh
 isofs isofs.sh
 
 fs_fill fs_fill
+
+binfmt_misc01 binfmt_misc01.sh
testcases/kernel/fs/binfmt_misc/Makefile new file +12 -0
new file mode 100644
index 0000000..f9819ad
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
+# Author: Xiao Yang <[email protected]>
+
+top_srcdir              ?= ../../../..
+
+include $(top_srcdir)/include/mk/env_pre.mk
+
+INSTALL_TARGETS         := binfmt_misc01.sh binfmt_misc_lib.sh
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh new file +16 -0
new file mode 100755
index 0000000..1e9e9b9
@@ -0,0 +1,65 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
+# Author: Xiao Yang <[email protected]>
+#
+# Description:
+# Use various invalid inputs to register a new binary type.
+# 1) Invalid format of string fails to register a new binary type.
+# 2) Invalid type fails to register a new binary type.
+# 3) Invalid name containing slashes fails to register a new
+#    binary type.
+# 4) If extension matching is chosen, invalid magic containing
+#    slashes fails to register a new binary type.
+# 5) If magic matching is chosen, invalid offset(e.g. -1 and
+#    2500000000) fails to register a new binary type.

testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh line 26

Quoted 10 lines
testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh new file +10 -0
+# 6) Invalid flag fails to register a new binary type.
+#
+# Note:
+# This is also a regression test for the following kernel bug:
+# '5cc41e099504 ("fs/binfmt_misc.c: do not allow offset overflow")'
+
+
+TST_CNT=9
+TST_TESTFUNC=do_test
+TST_NEEDS_CMDS="cat"

TST_NEEDS_ROOT=1 ?

testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh line 35

Quoted 8 lines
testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh new file +8 -0
+. binfmt_misc_lib.sh
+
+verify_binfmt_misc()
+{
+	local name=$(echo "$1" | awk -F ':' '{print $2}')
+	local mntpoint=$(get_binfmt_misc_mntpoint)
+
+	(echo "$1" >"$mntpoint/register") 2>/dev/null

We should check the return value here as well, it should be non-zero.

testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh line 45

Quoted 10 lines
testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh new file +10 -0
+	if [ ! -f "$mntpoint/$name" ]; then
+		tst_res TPASS "Failed to register a binary type"
+		return
+	fi
+
+	# Trigger kernel crash reliably by cat command.
+	cat "$mntpoint/$name" >/dev/null
+	tst_res TFAIL "Register a binary type successfully"
+
+	(echo -1 >"$mntpoint/$name") 2>/dev/null

Here as well, we should check the return value.

Quoted 59 lines
testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh new file +20 -0
+	[ -f "$mntpoint/$name" ] && \
+		tst_res TWARN "Failed to remove a binary type"
+}
+
+do_test()
+{
+	case $1 in
+	1) verify_binfmt_misc ".textension,E,,ltp,,$(which cat),";;
+	2) verify_binfmt_misc ":tnone:X::ltp::$(which cat):";;
+	3) verify_binfmt_misc ":textension/:E::ltp::$(which cat):";;
+	4) verify_binfmt_misc ":tmagic/:M::ltp::$(which cat):";;
+	5) verify_binfmt_misc ":textension:E::ltp/::$(which cat):";;
+	6) verify_binfmt_misc ":tmagic:M:-1:ltp::$(which cat):";;
+	7) verify_binfmt_misc ":tmagic:M:2500000000:ltp::$(which cat):";;
+	8) verify_binfmt_misc ":textension:E::ltp::$(which cat):A";;
+	9) verify_binfmt_misc ":tmagic:M::ltp::$(which cat):A";;
+	esac
+}
+
+tst_run
testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh new file +33 -0
new file mode 100755
index 0000000..d188601
@@ -0,0 +1,64 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
+# Author: Xiao Yang <[email protected]>
+
+TST_SETUP=binfmt_misc_setup
+TST_CLEANUP=binfmt_misc_cleanup
+TST_NEEDS_DRIVERS="binfmt_misc"
+TST_NEEDS_TMPDIR=1
+TST_NEEDS_CMDS="${TST_NEEDS_CMDS} modprobe mount umount mkdir rm"
+
+. tst_test.sh
+
+rmod_binfmt_misc=0
+umount_binfmt_misc=0
+binfmt_misc_mntpoint="ltp_binfmt_misc"
+
+get_binfmt_misc_mntpoint()
+{
+	local mntpoint
+
+	mntpoint=$(grep " binfmt_misc " /proc/mounts | awk '{ print $2 }')
+	[ -z "$mntpoint" ] && tst_brk TBROK "Can't get binfmt_misc mntpoint"
+
+	echo "$mntpoint"
+}
+
+binfmt_misc_setup()
+{
+	local mntpoint
+
+	if ! grep -q "binfmt_misc" /proc/filesystems; then

testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh line 43

Quoted 10 lines
testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh new file +10 -0
+		ROD modprobe binfmt_misc
+		rmod_binfmt_misc=1
+	fi
+
+	# Match fs type accurately, because autofs is also mounted on
+	# /proc/sys/fs/binfmt_misc on some distros, as below:
+	# cat /proc/mounts | grep binfmt_misc
+	# systemd-1 /proc/sys/fs/binfmt_misc autofs ...
+	# binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc ...
+	mntpoint=$(grep " binfmt_misc " /proc/mounts | awk '{ print $2 }')

You can do just: awk '/binfmt_misc/ { print $2 }' /proc/mounts

Quoted 11 lines
testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh new file +11 -0
+	[ -n "$mntpoint" ] && return
+
+	ROD mkdir ${binfmt_misc_mntpoint}
+	ROD mount -t binfmt_misc none ${binfmt_misc_mntpoint}
+	umount_binfmt_misc=1
+}
+
+binfmt_misc_cleanup()
+{
+	if [ ${umount_binfmt_misc} -ne 0 ]; then
+		umount ${binfmt_misc_mntpoint}

testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh line 64

Quoted 10 lines
testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh new file +10 -0
+		umount_binfmt_misc=0
+	fi
+
+	[ -d ${binfmt_misc_mntpoint} ] && rm -rf ${binfmt_misc_mntpoint}
+
+	if [ ${rmod_binfmt_misc} -ne 0 ]; then
+		modprobe -r binfmt_misc
+		rmod_binfmt_misc=0
+	fi
+}

Other than the minor things it looks fine.

Also it would be nice to have functional test as well, I guess that we can register /bin/sh to be interpreter for a file with specific magic and extension then execute a script that does just echo test or something like this.

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#2 lore raw

PATCH 1/1 fs/binfmt_misc01.sh: Add new test for invalid inputs

Hi Cyril,

Quoted 65 lines · Cyril Hrubis

Hi!

Quoted 64 lines
---
  runtest/fs                                         |  2 +
  testcases/kernel/fs/binfmt_misc/Makefile           | 12 ++++
  testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh   | 65 ++++++++++++++++++++++
  testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh | 64 +++++++++++++++++++++
  4 files changed, 143 insertions(+)

create mode 100644 testcases/kernel/fs/binfmt_misc/Makefile create mode 100755 testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh create mode 100755 testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh

runtest/fs +2 -0
index aca7e35..227c186 100644
@@ -82,3 +82,5 @@ quota_remount_test01 quota_remount_test01.sh
  isofs isofs.sh
 
  fs_fill fs_fill
+
+binfmt_misc01 binfmt_misc01.sh
testcases/kernel/fs/binfmt_misc/Makefile new file +12 -0
new file mode 100644
index 0000000..f9819ad
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
+# Author: Xiao Yang<[email protected]>
+
+top_srcdir              ?= ../../../..
+
+include $(top_srcdir)/include/mk/env_pre.mk
+
+INSTALL_TARGETS         := binfmt_misc01.sh binfmt_misc_lib.sh
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh new file +19 -0
new file mode 100755
index 0000000..1e9e9b9
@@ -0,0 +1,65 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
+# Author: Xiao Yang<[email protected]>
+#
+# Description:
+# Use various invalid inputs to register a new binary type.
+# 1) Invalid format of string fails to register a new binary type.
+# 2) Invalid type fails to register a new binary type.
+# 3) Invalid name containing slashes fails to register a new
+#    binary type.
+# 4) If extension matching is chosen, invalid magic containing
+#    slashes fails to register a new binary type.
+# 5) If magic matching is chosen, invalid offset(e.g. -1 and
+#    2500000000) fails to register a new binary type.
+# 6) Invalid flag fails to register a new binary type.
+#
+# Note:
Quoted 8 lines

testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh line 26

Quoted 7 lines
testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh new file +7 -0
+# This is also a regression test for the following kernel bug:
+# '5cc41e099504 ("fs/binfmt_misc.c: do not allow offset overflow")'
+
+
+TST_CNT=9
+TST_TESTFUNC=do_test
+TST_NEEDS_CMDS="cat"

TST_NEEDS_ROOT=1 ?

Yes, we need it.

Quoted 9 lines

testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh

Quoted 8 lines
testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh new file +8 -0
+. binfmt_misc_lib.sh
+
+verify_binfmt_misc()
+{
+	local name=$(echo "$1" | awk -F ':' '{print $2}')
+	local mntpoint=$(get_binfmt_misc_mntpoint)
+
+	(echo "$1">"$mntpoint/register") 2>/dev/null

We should check the return value here as well, it should be non-zero.

I will add this check.

Quoted 12 lines

testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh

Quoted 10 lines
testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh new file +10 -0
+	if [ ! -f "$mntpoint/$name" ]; then
+		tst_res TPASS "Failed to register a binary type"
+		return
+	fi
+
+	# Trigger kernel crash reliably by cat command.
+	cat "$mntpoint/$name">/dev/null
+	tst_res TFAIL "Register a binary type successfully"
+
+	(echo -1>"$mntpoint/$name") 2>/dev/null

Here as well, we should check the return value.

Here as well.

Quoted 62 lines
Quoted 62 lines
testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh new file +20 -0
+	[ -f "$mntpoint/$name" ]&&  \
+		tst_res TWARN "Failed to remove a binary type"
+}
+
+do_test()
+{
+	case $1 in
+	1) verify_binfmt_misc ".textension,E,,ltp,,$(which cat),";;
+	2) verify_binfmt_misc ":tnone:X::ltp::$(which cat):";;
+	3) verify_binfmt_misc ":textension/:E::ltp::$(which cat):";;
+	4) verify_binfmt_misc ":tmagic/:M::ltp::$(which cat):";;
+	5) verify_binfmt_misc ":textension:E::ltp/::$(which cat):";;
+	6) verify_binfmt_misc ":tmagic:M:-1:ltp::$(which cat):";;
+	7) verify_binfmt_misc ":tmagic:M:2500000000:ltp::$(which cat):";;
+	8) verify_binfmt_misc ":textension:E::ltp::$(which cat):A";;
+	9) verify_binfmt_misc ":tmagic:M::ltp::$(which cat):A";;
+	esac
+}
+
+tst_run
testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh new file +36 -0
new file mode 100755
index 0000000..d188601
@@ -0,0 +1,64 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
+# Author: Xiao Yang<[email protected]>
+
+TST_SETUP=binfmt_misc_setup
+TST_CLEANUP=binfmt_misc_cleanup
+TST_NEEDS_DRIVERS="binfmt_misc"
+TST_NEEDS_TMPDIR=1
+TST_NEEDS_CMDS="${TST_NEEDS_CMDS} modprobe mount umount mkdir rm"
+
+. tst_test.sh
+
+rmod_binfmt_misc=0
+umount_binfmt_misc=0
+binfmt_misc_mntpoint="ltp_binfmt_misc"
+
+get_binfmt_misc_mntpoint()
+{
+	local mntpoint
+
+	mntpoint=$(grep " binfmt_misc " /proc/mounts | awk '{ print $2 }')
+	[ -z "$mntpoint" ]&&  tst_brk TBROK "Can't get binfmt_misc mntpoint"
+
+	echo "$mntpoint"
+}
+
+binfmt_misc_setup()
+{
+	local mntpoint
+
+	if ! grep -q "binfmt_misc" /proc/filesystems; then
+		ROD modprobe binfmt_misc
+		rmod_binfmt_misc=1
+	fi
Quoted 8 lines

testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh line 43

Quoted 7 lines
testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh new file +7 -0
+
+	# Match fs type accurately, because autofs is also mounted on
+	# /proc/sys/fs/binfmt_misc on some distros, as below:
+	# cat /proc/mounts | grep binfmt_misc
+	# systemd-1 /proc/sys/fs/binfmt_misc autofs ...
+	# binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc ...
+	mntpoint=$(grep " binfmt_misc " /proc/mounts | awk '{ print $2 }')

You can do just: awk '/binfmt_misc/ { print $2 }' /proc/mounts

It is simpler to me.

Quoted 23 lines
Quoted 11 lines
testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh new file +11 -0
+	[ -n "$mntpoint" ]&&  return
+
+	ROD mkdir ${binfmt_misc_mntpoint}
+	ROD mount -t binfmt_misc none ${binfmt_misc_mntpoint}
+	umount_binfmt_misc=1
+}
+
+binfmt_misc_cleanup()
+{
+	if [ ${umount_binfmt_misc} -ne 0 ]; then
+		umount ${binfmt_misc_mntpoint}

testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh line 64

Quoted 10 lines
testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh new file +10 -0
+		umount_binfmt_misc=0
+	fi
+
+	[ -d ${binfmt_misc_mntpoint} ]&&  rm -rf ${binfmt_misc_mntpoint}
+
+	if [ ${rmod_binfmt_misc} -ne 0 ]; then
+		modprobe -r binfmt_misc
+		rmod_binfmt_misc=0
+	fi
+}

Other than the minor things it looks fine.

Quoted 4 lines

Also it would be nice to have functional test as well, I guess that we can register /bin/sh to be interpreter for a file with specific magic and extension then execute a script that does just echo test or something like this.

Thanks for your review and suggestion. :-)
I will send the v2 patch and try to write functional test for binfmt_misc.

Best Regards,
Xiao Yang

author
#3 lore raw

PATCH v2 1/1 fs/binfmt_misc01.sh: Add new test for invalid inputs

In reply to Cyril Hrubis

Use various invalid inputs to register a new binary type.

Note:
This is also a regression test for the following kernel bug:
'5cc41e099504 ("fs/binfmt_misc.c: do not allow offset overflow")

---
 runtest/fs                                         |  2 +
 testcases/kernel/fs/binfmt_misc/Makefile           | 12 ++++
 testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh   | 67 ++++++++++++++++++++++
 testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh | 65 +++++++++++++++++++++
 4 files changed, 146 insertions(+)
 create mode 100644 testcases/kernel/fs/binfmt_misc/Makefile
 create mode 100755 testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh
 create mode 100755 testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh
runtest/fs +2 -0
index aca7e35..227c186 100644
@@ -82,3 +82,5 @@ quota_remount_test01 quota_remount_test01.sh
 isofs isofs.sh
 
 fs_fill fs_fill
+
+binfmt_misc01 binfmt_misc01.sh
testcases/kernel/fs/binfmt_misc/Makefile new file +12 -0
new file mode 100644
index 0000000..f9819ad
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
+# Author: Xiao Yang <[email protected]>
+
+top_srcdir              ?= ../../../..
+
+include $(top_srcdir)/include/mk/env_pre.mk
+
+INSTALL_TARGETS         := binfmt_misc01.sh binfmt_misc_lib.sh
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/fs/binfmt_misc/binfmt_misc01.sh new file +67 -0

67 lines not on this page · raw message

testcases/kernel/fs/binfmt_misc/binfmt_misc_lib.sh new file +65 -0

65 lines not on this page · raw message

Signature · 1 line
1.8.3.1