fstests.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/5] fstests: recent series rollup / misc tests & fixes
@ 2020-05-18 16:10 Eric Sandeen
  2020-05-18 16:11 ` [PATCH 1/5] fstests: add _require_sysctl_variable helper Eric Sandeen
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: Eric Sandeen @ 2020-05-18 16:10 UTC (permalink / raw)
  To: fstests

Resending the last few of my patches as a series, with requested
fixes from Eryu, plus one more new one.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/5] fstests: add _require_sysctl_variable helper
  2020-05-18 16:10 [PATCH 0/5] fstests: recent series rollup / misc tests & fixes Eric Sandeen
@ 2020-05-18 16:11 ` Eric Sandeen
  2020-05-18 16:14 ` [PATCH 2/5] fstests: test restricted symlinks & hardlinks sysctls Eric Sandeen
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Eric Sandeen @ 2020-05-18 16:11 UTC (permalink / raw)
  To: fstests

New _require_sysctl_variable test to ensure that the sysctl we wish to
test is available on the system.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
---

Next 2 patches will use this.

V2: rename to "_require_sysctl_variable" (sysctl man page calls these
"variables")

 common/rc | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/common/rc b/common/rc
index b25cad0e..62952036 100644
--- a/common/rc
+++ b/common/rc
@@ -4209,6 +4209,12 @@ _require_bsd_process_accounting()
 	$ACCTON_PROG off >> $seqres.full
 }
 
+_require_sysctl_variable()
+{
+	local name=$1
+	sysctl $name &>/dev/null || _notrun "$name sysctl unavailable"
+}
+
 init_rc
 
 ################################################################################
-- 
2.17.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 2/5] fstests: test restricted symlinks & hardlinks sysctls
  2020-05-18 16:10 [PATCH 0/5] fstests: recent series rollup / misc tests & fixes Eric Sandeen
  2020-05-18 16:11 ` [PATCH 1/5] fstests: add _require_sysctl_variable helper Eric Sandeen
@ 2020-05-18 16:14 ` Eric Sandeen
  2020-05-24 15:16   ` Eryu Guan
  2020-05-18 16:15 ` [PATCH 3/5] fstests: test restricted file access sysctls Eric Sandeen
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 8+ messages in thread
From: Eric Sandeen @ 2020-05-18 16:14 UTC (permalink / raw)
  To: fstests

This tests the fs.protected_symlinks and fs.protected_hardlinks
sysctls which restrict links behavior in sticky world-writable
directories as documented in the kernel at
Documentation/admin-guide/sysctl/fs.rst

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
---

V2: many fixes requested by Eryu
update copyright
reset sysctl only if saved
switch to _user_do
fix test description in comments

 tests/generic/900     | 115 ++++++++++++++++++++++++++++++++++++++++++
 tests/generic/900.out |  14 +++++
 tests/generic/group   |   1 +
 3 files changed, 130 insertions(+)
 create mode 100755 tests/generic/900
 create mode 100644 tests/generic/900.out

diff --git a/tests/generic/900 b/tests/generic/900
new file mode 100755
index 00000000..fd54fa4e
--- /dev/null
+++ b/tests/generic/900
@@ -0,0 +1,115 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2020 Red Hat, Inc.  All Rights Reserved.
+#
+# FS QA Test 900
+#
+# Test protected_symlink and protected_hardlink sysctls
+#
+seq=`basename $0`
+seqres=$RESULT_DIR/$seq
+echo "QA output created by $seq"
+
+here=`pwd`
+tmp=/tmp/$$
+status=1	# failure is the default!
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+_cleanup()
+{
+	rm -rf $TEST_DIR/$seq
+	[ ! -z "$SYMLINK_PROTECTION" ] \
+		&& sysctl -qw fs.protected_symlinks=$SYMLINK_PROTECTION
+	[ ! -z "$HARDLINK_PROTECTION" ] \
+		&& sysctl -qw fs.protected_hardlinks=$HARDLINK_PROTECTION
+	cd /
+	rm -f $tmp.*
+}
+
+# get standard environment, filters and checks
+. ./common/rc
+. ./common/filter
+
+# remove previous $seqres.full before test
+rm -f $seqres.full
+
+# real QA test starts here
+
+# Modify as appropriate.
+_supported_fs generic
+_supported_os Linux
+_require_test
+_require_sysctl_variable fs.protected_symlinks
+_require_sysctl_variable fs.protected_hardlinks
+_require_user 123456-fsgqa
+# Do this SECOND so that qa_user is fsgqa, and _do_user uses that account
+_require_user fsgqa
+
+OWNER=123456-fsgqa
+OTHER=fsgqa
+
+# Save current system state to reset when done
+SYMLINK_PROTECTION=`sysctl -n fs.protected_symlinks`
+HARDLINK_PROTECTION=`sysctl -n fs.protected_hardlinks`
+
+test_symlink()
+{
+	ln -s $TEST_DIR/$seq/target $TEST_DIR/$seq/sticky_dir/symlink
+	chown $OTHER.$OTHER $TEST_DIR/$seq/sticky_dir
+	chown $OWNER.$OWNER $TEST_DIR/$seq/sticky_dir/symlink
+	# If we can read the target, we followed the link
+	_user_do "cat $TEST_DIR/$seq/sticky_dir/symlink" | _filter_test_dir
+	rm -f $TEST_DIR/$seq/sticky_dir/symlink
+}
+
+test_hardlink()
+{
+	chown $OWNER.$OWNER $TEST_DIR/$seq/target
+	chmod go-rw $TEST_DIR/$seq/target
+	_user_do "ln $TEST_DIR/$seq/target $TEST_DIR/$seq/sticky_dir/hardlink" \
+		| _filter_test_dir
+	test -f $TEST_DIR/$seq/sticky_dir/hardlink \
+		&& echo "successfully created hardlink"
+	rm -f $TEST_DIR/$seq/sticky_dir/hardlink
+}
+
+setup_tree()
+{
+	# Create world-writable sticky dir
+	mkdir -p $TEST_DIR/$seq/sticky_dir
+	chmod 1777 $TEST_DIR/$seq/sticky_dir
+	# And a file elsewhere that will be linked to from that sticky dir
+	mkdir -p $TEST_DIR/$seq
+	# If we can read it, we followed the link.
+	echo "successfully followed symlink" > $TEST_DIR/$seq/target
+}
+
+setup_tree
+
+# First test fs.protected_symlinks
+# With protection on, symlink follows should fail if the
+# link owner != the sticky directory owner, and the process
+# is not the link owner.
+echo "== Test symlink follow protection when"
+echo "== process != link owner and dir owner != link owner"
+sysctl -w fs.protected_symlinks=0
+test_symlink
+sysctl -w fs.protected_symlinks=1
+test_symlink
+
+echo
+
+# Now test fs.protected_hardlinks
+# With protection on, hardlink creation should fail if the
+# process does not own the target file, and the process does not have
+# read-write access to the target
+echo "== Test hardlink create protection when"
+echo "== process != target owner and process cannot read target"
+sysctl -w fs.protected_hardlinks=0
+test_hardlink
+sysctl -w fs.protected_hardlinks=1
+test_hardlink
+
+# success, all done
+status=0
+exit
diff --git a/tests/generic/900.out b/tests/generic/900.out
new file mode 100644
index 00000000..7adf97ed
--- /dev/null
+++ b/tests/generic/900.out
@@ -0,0 +1,14 @@
+QA output created by 900
+== Test symlink follow protection when
+== process != link owner and dir owner != link owner
+fs.protected_symlinks = 0
+successfully followed symlink
+fs.protected_symlinks = 1
+Permission denied
+
+== Test hardlink create protection when
+== process != target owner and process cannot read target
+fs.protected_hardlinks = 0
+successfully created hardlink
+fs.protected_hardlinks = 1
+ln: failed to create hard link 'TEST_DIR/900/sticky_dir/hardlink' => 'TEST_DIR/900/target': Operation not permitted
diff --git a/tests/generic/group b/tests/generic/group
index e82004e8..fd2360ea 100644
--- a/tests/generic/group
+++ b/tests/generic/group
@@ -599,3 +599,4 @@
 594 auto quick quota
 595 auto quick encrypt
 596 auto quick
+900 auto quick perms
-- 
2.17.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 3/5] fstests: test restricted file access sysctls
  2020-05-18 16:10 [PATCH 0/5] fstests: recent series rollup / misc tests & fixes Eric Sandeen
  2020-05-18 16:11 ` [PATCH 1/5] fstests: add _require_sysctl_variable helper Eric Sandeen
  2020-05-18 16:14 ` [PATCH 2/5] fstests: test restricted symlinks & hardlinks sysctls Eric Sandeen
@ 2020-05-18 16:15 ` Eric Sandeen
  2020-05-18 16:16 ` [PATCH 4/5] fstests: add _require_mknod Eric Sandeen
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Eric Sandeen @ 2020-05-18 16:15 UTC (permalink / raw)
  To: fstests

This tests the fs.protected_regular and fs.protected_fifos
sysctls which restrict access behavior in sticky world-writable
directories as documented in the kernel at
Documentation/admin-guide/sysctl/fs.rst

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
---

V2: Same fixes as prior patch

 tests/generic/901     | 126 ++++++++++++++++++++++++++++++++++++++++++
 tests/generic/901.out |  28 ++++++++++
 tests/generic/group   |   1 +
 3 files changed, 155 insertions(+)
 create mode 100755 tests/generic/901
 create mode 100644 tests/generic/901.out

diff --git a/tests/generic/901 b/tests/generic/901
new file mode 100755
index 00000000..4e04155e
--- /dev/null
+++ b/tests/generic/901
@@ -0,0 +1,126 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2020 Red Hat, Inc.  All Rights Reserved.
+#
+# FS QA Test 901
+#
+# Test protected_regular and protected_fifos sysctls
+#
+seq=`basename $0`
+seqres=$RESULT_DIR/$seq
+echo "QA output created by $seq"
+
+here=`pwd`
+tmp=/tmp/$$
+status=1	# failure is the default!
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+_cleanup()
+{
+	rm -rf $TEST_DIR/$seq
+	[ ! -z "$REGULAR_PROTECTION" ] \
+		&& sysctl -qw fs.protected_regular=$REGULAR_PROTECTION
+	[ ! -z "$FIFO_PROTECTION" ] \
+		&& sysctl -qw fs.protected_fifos=$FIFO_PROTECTION
+	cd /
+	rm -f $tmp.*
+}
+
+# get standard environment, filters and checks
+. ./common/rc
+. ./common/filter
+
+# remove previous $seqres.full before test
+rm -f $seqres.full
+
+# real QA test starts here
+
+# Modify as appropriate.
+_supported_fs generic
+_supported_os Linux
+_require_test
+_require_sysctl_variable fs.protected_regular
+_require_sysctl_variable fs.protected_fifos
+_require_user 123456-fsgqa 
+# Do this SECOND so that qa_user is fsgqa, and _do_user uses that account
+_require_user fsgqa
+
+USER1=123456-fsgqa
+USER2=fsgqa
+
+# Save current system state to reset when done
+REGULAR_PROTECTION=`sysctl -n fs.protected_regular`
+FIFO_PROTECTION=`sysctl -n fs.protected_fifos`
+
+test_access()
+{
+	FILENAME=$1
+
+	# sticky dir is world & group writable:
+	echo "= group & world writable dir"
+	chmod og+w $TEST_DIR/$seq/sticky_dir
+	# "open -f" opens O_CREAT
+	_user_do "$XFS_IO_PROG -c \"open -f $TEST_DIR/$seq/sticky_dir/$FILENAME\""
+	# sticky dir is only group writable:
+	echo "= only group writable dir"
+	chmod o-w $TEST_DIR/$seq/sticky_dir
+	_user_do "$XFS_IO_PROG -c \"open -f $TEST_DIR/$seq/sticky_dir/$FILENAME\""
+}
+
+setup_tree()
+{
+	# Create sticky dir owned by $USER2
+	mkdir -p $TEST_DIR/$seq
+	mkdir -p $TEST_DIR/$seq/sticky_dir
+	chmod 1777 $TEST_DIR/$seq/sticky_dir
+	chown $USER2.$USER2 $TEST_DIR/$seq/sticky_dir
+
+	# Create file & fifo in that dir owned by $USER1, and open
+	# normal read/write privs for world & group
+	$XFS_IO_PROG -c "open -f $TEST_DIR/$seq/sticky_dir/file"
+	chown $USER1.$USER1 $TEST_DIR/$seq/sticky_dir/file
+	chmod o+rw $TEST_DIR/$seq/sticky_dir/file
+
+	mkfifo $TEST_DIR/$seq/sticky_dir/fifo
+	chown $USER1.$USER1 $TEST_DIR/$seq/sticky_dir/fifo
+	chmod o+rw $TEST_DIR/$seq/sticky_dir/fifo
+}
+
+setup_tree
+
+# First test fs.protected_regular
+# With protection set to 1, O_CREAT opens in a world-writable sticky
+# directory should fail if the file exists, is owned by another, and
+# file owner != dir owner
+#
+# With protection set to 2, the same goes for group-writable
+# sticky directories
+
+echo "== Test file open when owned by another and file owner != dir owner"
+sysctl -w fs.protected_regular=0
+test_access file
+sysctl -w fs.protected_regular=1
+test_access file
+sysctl -w fs.protected_regular=2
+test_access file
+
+echo
+
+# Now test fs.protected_fifos
+# With protection set to 1, O_CREAT opens in a world-writable sticky
+# directory should fail if the fifo exists, is owned by another, and
+# file owner != dir owner
+#
+# With protection set to 2, the same goes for group-writable
+# sticky directories
+echo "== Test fifo open when owned by another and fifo owner != dir owner"
+sysctl -w fs.protected_fifos=0
+test_access fifo
+sysctl -w fs.protected_fifos=1
+test_access fifo
+sysctl -w fs.protected_fifos=2
+test_access fifo
+
+# success, all done
+status=0
+exit
diff --git a/tests/generic/901.out b/tests/generic/901.out
new file mode 100644
index 00000000..5f95d9be
--- /dev/null
+++ b/tests/generic/901.out
@@ -0,0 +1,28 @@
+QA output created by 901
+== Test file open when owned by another and file owner != dir owner
+fs.protected_regular = 0
+= group & world writable dir
+= only group writable dir
+fs.protected_regular = 1
+= group & world writable dir
+Permission denied
+= only group writable dir
+fs.protected_regular = 2
+= group & world writable dir
+Permission denied
+= only group writable dir
+Permission denied
+
+== Test fifo open when owned by another and fifo owner != dir owner
+fs.protected_fifos = 0
+= group & world writable dir
+= only group writable dir
+fs.protected_fifos = 1
+= group & world writable dir
+Permission denied
+= only group writable dir
+fs.protected_fifos = 2
+= group & world writable dir
+Permission denied
+= only group writable dir
+Permission denied
diff --git a/tests/generic/group b/tests/generic/group
index fd2360ea..50c340a6 100644
--- a/tests/generic/group
+++ b/tests/generic/group
@@ -600,3 +600,4 @@
 595 auto quick encrypt
 596 auto quick
 900 auto quick perms
+901 auto quick perms
-- 
2.17.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 4/5] fstests: add _require_mknod
  2020-05-18 16:10 [PATCH 0/5] fstests: recent series rollup / misc tests & fixes Eric Sandeen
                   ` (2 preceding siblings ...)
  2020-05-18 16:15 ` [PATCH 3/5] fstests: test restricted file access sysctls Eric Sandeen
@ 2020-05-18 16:16 ` Eric Sandeen
  2020-05-18 16:18 ` [PATCH 5/5] xfstests: check for unknown flag result in _require_xfs_io_command Eric Sandeen
  2020-05-21  9:38 ` [PATCH 0/5] fstests: recent series rollup / misc tests & fixes Zorro Lang
  5 siblings, 0 replies; 8+ messages in thread
From: Eric Sandeen @ 2020-05-18 16:16 UTC (permalink / raw)
  To: fstests

Add a _require_mknod test so that filesystems with no ->mknod
operation will _notrun instead of fail, and add the helper to
all necessary tests.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
---
 common/rc         | 7 +++++++
 tests/generic/062 | 1 +
 tests/generic/184 | 1 +
 tests/generic/306 | 1 +
 tests/generic/401 | 1 +
 tests/generic/434 | 1 +
 tests/generic/479 | 1 +
 tests/generic/564 | 3 +++
 8 files changed, 16 insertions(+)

diff --git a/common/rc b/common/rc
index 62952036..310fdc46 100644
--- a/common/rc
+++ b/common/rc
@@ -4215,6 +4215,13 @@ _require_sysctl_variable()
 	sysctl $name &>/dev/null || _notrun "$name sysctl unavailable"
 }
 
+_require_mknod()
+{
+	mknod $TEST_DIR/$seq.null c 1 3 \
+		|| _notrun "$FSTYP does not support mknod/mkfifo"
+	rm -f $TEST_DIR/$seq.null
+}
+
 init_rc
 
 ################################################################################
diff --git a/tests/generic/062 b/tests/generic/062
index ba744885..d2a0ac9a 100755
--- a/tests/generic/062
+++ b/tests/generic/062
@@ -60,6 +60,7 @@ _supported_os Linux
 _require_scratch
 _require_attrs
 _require_symlinks
+_require_mknod
 
 rm -f $tmp.backup1 $tmp.backup2 $seqres.full
 
diff --git a/tests/generic/184 b/tests/generic/184
index 8aca1a0d..94f4d0e1 100755
--- a/tests/generic/184
+++ b/tests/generic/184
@@ -29,6 +29,7 @@ _cleanup()
 _supported_fs generic
 _supported_os Linux
 _require_test
+_require_mknod
 
 rm -f $TEST_DIR/null
 mknod $TEST_DIR/null c 1 3
diff --git a/tests/generic/306 b/tests/generic/306
index 046f4516..6f16a5f5 100755
--- a/tests/generic/306
+++ b/tests/generic/306
@@ -34,6 +34,7 @@ _supported_os Linux
 _require_scratch
 _require_test
 _require_symlinks
+_require_mknod
 
 DEVNULL=$SCRATCH_MNT/devnull
 DEVZERO=$SCRATCH_MNT/devzero
diff --git a/tests/generic/401 b/tests/generic/401
index 00e9bbe0..f8b1fd21 100755
--- a/tests/generic/401
+++ b/tests/generic/401
@@ -38,6 +38,7 @@ _supported_fs generic
 _supported_os Linux
 _require_scratch
 _require_symlinks
+_require_mknod
 _require_test_program "t_dir_type"
 
 rm -f $seqres.full
diff --git a/tests/generic/434 b/tests/generic/434
index edbf49d3..131c5d99 100755
--- a/tests/generic/434
+++ b/tests/generic/434
@@ -30,6 +30,7 @@ _supported_os Linux
 
 _require_xfs_io_command "copy_range"
 _require_test
+_require_mknod
 
 testdir=$TEST_DIR/test-$seq
 rm -rf $testdir
diff --git a/tests/generic/479 b/tests/generic/479
index aaf864a4..fc0b3b6c 100755
--- a/tests/generic/479
+++ b/tests/generic/479
@@ -33,6 +33,7 @@ _supported_fs generic
 _supported_os Linux
 _require_scratch
 _require_symlinks
+_require_mknod
 _require_dm_target flakey
 
 rm -f $seqres.full
diff --git a/tests/generic/564 b/tests/generic/564
index 4958b3b5..b9b21134 100755
--- a/tests/generic/564
+++ b/tests/generic/564
@@ -37,6 +37,9 @@ rm -f $seqres.full
 
 _require_test
 _require_loop
+# for mkfifo
+_require_mknod
+
 #
 # This test effectively requires xfs_io with these commits
 #  2a42470b xfs_io: copy_file_range length is a size_t
-- 
2.17.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 5/5] xfstests: check for unknown flag result in _require_xfs_io_command
  2020-05-18 16:10 [PATCH 0/5] fstests: recent series rollup / misc tests & fixes Eric Sandeen
                   ` (3 preceding siblings ...)
  2020-05-18 16:16 ` [PATCH 4/5] fstests: add _require_mknod Eric Sandeen
@ 2020-05-18 16:18 ` Eric Sandeen
  2020-05-21  9:38 ` [PATCH 0/5] fstests: recent series rollup / misc tests & fixes Zorro Lang
  5 siblings, 0 replies; 8+ messages in thread
From: Eric Sandeen @ 2020-05-18 16:18 UTC (permalink / raw)
  To: fstests

If we are testing for "xfs_io -c chattr $FOO" be sure to catch any
unknown flag output and _notrun

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
---
 common/rc | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/common/rc b/common/rc
index 310fdc46..a6967831 100644
--- a/common/rc
+++ b/common/rc
@@ -2255,6 +2255,8 @@ _require_xfs_io_command()
 		_notrun "xfs_io $command $param_checked not supported on $FSTYP"
 	echo $testio | grep -q "Function not implemented" && \
 		_notrun "xfs_io $command $param_checked support is missing (missing syscall?)"
+	echo $testio | grep -q "unknown flag" && \
+		_notrun "xfs_io $command $param_checked support is missing (unknown flag)"
 
 	[ -n "$param" ] || return
 
-- 
2.17.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 0/5] fstests: recent series rollup / misc tests & fixes
  2020-05-18 16:10 [PATCH 0/5] fstests: recent series rollup / misc tests & fixes Eric Sandeen
                   ` (4 preceding siblings ...)
  2020-05-18 16:18 ` [PATCH 5/5] xfstests: check for unknown flag result in _require_xfs_io_command Eric Sandeen
@ 2020-05-21  9:38 ` Zorro Lang
  5 siblings, 0 replies; 8+ messages in thread
From: Zorro Lang @ 2020-05-21  9:38 UTC (permalink / raw)
  To: fstests

On Mon, May 18, 2020 at 11:10:16AM -0500, Eric Sandeen wrote:
> Resending the last few of my patches as a series, with requested
> fixes from Eryu, plus one more new one.

Just a tiny problem as [1]. Others looks good to me.

Reviewed-by: Zorro Lang <zlang@redhat.com>

Thanks,
Zorro

[1]
Applying: fstests: test restricted file access sysctls
/root/git/xfstests-dev/.git/rebase-apply/patch:61: trailing whitespace.
_require_user 123456-fsgqa
warning: 1 line adds whitespace errors.

> 


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/5] fstests: test restricted symlinks & hardlinks sysctls
  2020-05-18 16:14 ` [PATCH 2/5] fstests: test restricted symlinks & hardlinks sysctls Eric Sandeen
@ 2020-05-24 15:16   ` Eryu Guan
  0 siblings, 0 replies; 8+ messages in thread
From: Eryu Guan @ 2020-05-24 15:16 UTC (permalink / raw)
  To: Eric Sandeen; +Cc: fstests

On Mon, May 18, 2020 at 11:14:43AM -0500, Eric Sandeen wrote:
> This tests the fs.protected_symlinks and fs.protected_hardlinks
> sysctls which restrict links behavior in sticky world-writable
> directories as documented in the kernel at
> Documentation/admin-guide/sysctl/fs.rst
> 
> Signed-off-by: Eric Sandeen <sandeen@redhat.com>
> ---
> 
> V2: many fixes requested by Eryu
> update copyright
> reset sysctl only if saved
> switch to _user_do
> fix test description in comments

Thanks a lot for the update, Eric!

> 
>  tests/generic/900     | 115 ++++++++++++++++++++++++++++++++++++++++++
>  tests/generic/900.out |  14 +++++
>  tests/generic/group   |   1 +
>  3 files changed, 130 insertions(+)
>  create mode 100755 tests/generic/900
>  create mode 100644 tests/generic/900.out
> 
> diff --git a/tests/generic/900 b/tests/generic/900
> new file mode 100755
> index 00000000..fd54fa4e
> --- /dev/null
> +++ b/tests/generic/900
> @@ -0,0 +1,115 @@
> +#! /bin/bash
> +# SPDX-License-Identifier: GPL-2.0
> +# Copyright (c) 2020 Red Hat, Inc.  All Rights Reserved.
> +#
> +# FS QA Test 900
> +#
> +# Test protected_symlink and protected_hardlink sysctls
> +#
> +seq=`basename $0`
> +seqres=$RESULT_DIR/$seq
> +echo "QA output created by $seq"
> +
> +here=`pwd`
> +tmp=/tmp/$$
> +status=1	# failure is the default!
> +trap "_cleanup; exit \$status" 0 1 2 3 15
> +
> +_cleanup()
> +{
> +	rm -rf $TEST_DIR/$seq
> +	[ ! -z "$SYMLINK_PROTECTION" ] \
> +		&& sysctl -qw fs.protected_symlinks=$SYMLINK_PROTECTION
> +	[ ! -z "$HARDLINK_PROTECTION" ] \
> +		&& sysctl -qw fs.protected_hardlinks=$HARDLINK_PROTECTION
> +	cd /
> +	rm -f $tmp.*
> +}
> +
> +# get standard environment, filters and checks
> +. ./common/rc
> +. ./common/filter
> +
> +# remove previous $seqres.full before test
> +rm -f $seqres.full
> +
> +# real QA test starts here
> +
> +# Modify as appropriate.
> +_supported_fs generic
> +_supported_os Linux
> +_require_test
> +_require_sysctl_variable fs.protected_symlinks
> +_require_sysctl_variable fs.protected_hardlinks
> +_require_user 123456-fsgqa

The su in _require_user prints warnings about user name starts with
digit and test fails like:

 + User or group name "123456-fsgqa" starts with a digit, accepting for compatibility.

So I discarded the output of _require_user here, also in generic/901.

_require_user 123456-fsgqa >/dev/null 2>&1

Thanks,
Eryu

> +# Do this SECOND so that qa_user is fsgqa, and _do_user uses that account
> +_require_user fsgqa
> +
> +OWNER=123456-fsgqa
> +OTHER=fsgqa
> +
> +# Save current system state to reset when done
> +SYMLINK_PROTECTION=`sysctl -n fs.protected_symlinks`
> +HARDLINK_PROTECTION=`sysctl -n fs.protected_hardlinks`
> +
> +test_symlink()
> +{
> +	ln -s $TEST_DIR/$seq/target $TEST_DIR/$seq/sticky_dir/symlink
> +	chown $OTHER.$OTHER $TEST_DIR/$seq/sticky_dir
> +	chown $OWNER.$OWNER $TEST_DIR/$seq/sticky_dir/symlink
> +	# If we can read the target, we followed the link
> +	_user_do "cat $TEST_DIR/$seq/sticky_dir/symlink" | _filter_test_dir
> +	rm -f $TEST_DIR/$seq/sticky_dir/symlink
> +}
> +
> +test_hardlink()
> +{
> +	chown $OWNER.$OWNER $TEST_DIR/$seq/target
> +	chmod go-rw $TEST_DIR/$seq/target
> +	_user_do "ln $TEST_DIR/$seq/target $TEST_DIR/$seq/sticky_dir/hardlink" \
> +		| _filter_test_dir
> +	test -f $TEST_DIR/$seq/sticky_dir/hardlink \
> +		&& echo "successfully created hardlink"
> +	rm -f $TEST_DIR/$seq/sticky_dir/hardlink
> +}
> +
> +setup_tree()
> +{
> +	# Create world-writable sticky dir
> +	mkdir -p $TEST_DIR/$seq/sticky_dir
> +	chmod 1777 $TEST_DIR/$seq/sticky_dir
> +	# And a file elsewhere that will be linked to from that sticky dir
> +	mkdir -p $TEST_DIR/$seq
> +	# If we can read it, we followed the link.
> +	echo "successfully followed symlink" > $TEST_DIR/$seq/target
> +}
> +
> +setup_tree
> +
> +# First test fs.protected_symlinks
> +# With protection on, symlink follows should fail if the
> +# link owner != the sticky directory owner, and the process
> +# is not the link owner.
> +echo "== Test symlink follow protection when"
> +echo "== process != link owner and dir owner != link owner"
> +sysctl -w fs.protected_symlinks=0
> +test_symlink
> +sysctl -w fs.protected_symlinks=1
> +test_symlink
> +
> +echo
> +
> +# Now test fs.protected_hardlinks
> +# With protection on, hardlink creation should fail if the
> +# process does not own the target file, and the process does not have
> +# read-write access to the target
> +echo "== Test hardlink create protection when"
> +echo "== process != target owner and process cannot read target"
> +sysctl -w fs.protected_hardlinks=0
> +test_hardlink
> +sysctl -w fs.protected_hardlinks=1
> +test_hardlink
> +
> +# success, all done
> +status=0
> +exit
> diff --git a/tests/generic/900.out b/tests/generic/900.out
> new file mode 100644
> index 00000000..7adf97ed
> --- /dev/null
> +++ b/tests/generic/900.out
> @@ -0,0 +1,14 @@
> +QA output created by 900
> +== Test symlink follow protection when
> +== process != link owner and dir owner != link owner
> +fs.protected_symlinks = 0
> +successfully followed symlink
> +fs.protected_symlinks = 1
> +Permission denied
> +
> +== Test hardlink create protection when
> +== process != target owner and process cannot read target
> +fs.protected_hardlinks = 0
> +successfully created hardlink
> +fs.protected_hardlinks = 1
> +ln: failed to create hard link 'TEST_DIR/900/sticky_dir/hardlink' => 'TEST_DIR/900/target': Operation not permitted
> diff --git a/tests/generic/group b/tests/generic/group
> index e82004e8..fd2360ea 100644
> --- a/tests/generic/group
> +++ b/tests/generic/group
> @@ -599,3 +599,4 @@
>  594 auto quick quota
>  595 auto quick encrypt
>  596 auto quick
> +900 auto quick perms
> -- 
> 2.17.0
> 

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2020-05-24 15:17 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-18 16:10 [PATCH 0/5] fstests: recent series rollup / misc tests & fixes Eric Sandeen
2020-05-18 16:11 ` [PATCH 1/5] fstests: add _require_sysctl_variable helper Eric Sandeen
2020-05-18 16:14 ` [PATCH 2/5] fstests: test restricted symlinks & hardlinks sysctls Eric Sandeen
2020-05-24 15:16   ` Eryu Guan
2020-05-18 16:15 ` [PATCH 3/5] fstests: test restricted file access sysctls Eric Sandeen
2020-05-18 16:16 ` [PATCH 4/5] fstests: add _require_mknod Eric Sandeen
2020-05-18 16:18 ` [PATCH 5/5] xfstests: check for unknown flag result in _require_xfs_io_command Eric Sandeen
2020-05-21  9:38 ` [PATCH 0/5] fstests: recent series rollup / misc tests & fixes Zorro Lang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).