Skip to contentSkip to conversation

fstests: test restricted file access sysctls

PATCH Superseded Eric Sandeen 4 participants 18 messages May 5, 2020 – May 18, 2020

lore
Share & tools
lore
#0 lore raw

cover fstests: test restricted file access sysctls

This is a series of tests which test sysctls which restrict file access under certain conditions:

fs.restricted_symlinks
fs.restricted_hardlinks

and

fs.restricted_fifos
fs.restricted_regular

#1 lore raw

PATCH 1/3 fstests: add _require_sysctl helper

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

---

common/rc +6 -0
index 2000bd9d..2734dbf6 100644
@@ -4208,6 +4208,12 @@ _require_bsd_process_accounting()
 	$ACCTON_PROG off >> $seqres.full
 }
 
+_require_sysctl()
+{
+	name=$1
+	sysctl $name &>/dev/null || _notrun "$name sysctl unavailable"
+}
+
 init_rc
 
 ################################################################################
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] fstests: add _require_sysctl helper

Quoted 5 lines · Eric Sandeen

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

---

Quoted 19 lines
common/rc +6 -0
index 2000bd9d..2734dbf6 100644
@@ -4208,6 +4208,12 @@ _require_bsd_process_accounting()
 	$ACCTON_PROG off >> $seqres.full
 }
 
+_require_sysctl()
+{
+	name=$1
+	sysctl $name &>/dev/null || _notrun "$name sysctl unavailable"
+}
+
 init_rc
 
 ################################################################################
#3 lore raw

Re: [PATCH 1/3] fstests: add _require_sysctl helper

In reply to Eric Sandeen
Quoted 11 lines · Eric Sandeen

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

---

common/rc +0 -0
index 2000bd9d..2734dbf6 100644

common/rc _require_bsd_process_accounting() · line 4211

Quoted 4 lines
common/rc +1 -0
@@ -4208,6 +4208,12 @@ _require_bsd_process_accounting()
 	$ACCTON_PROG off >> $seqres.full
 }
 
+_require_sysctl()

IMHO, this name seems a bit confusing, it looks like it requires the sysctl command itself. How about _require_sysctl_entry ?

common/rc _require_bsd_process_accounting() · line 4213

Quoted 2 lines
common/rc +2 -0
+{
+	name=$1

Declare local var with 'local'.

common/rc _require_bsd_process_accounting() · line 4215

Quoted 2 lines
common/rc +2 -0
+	sysctl $name &>/dev/null || _notrun "$name sysctl unavailable"
+}

And I think this patch could be folded into the first test that takes use of it, i.e. patch 2.

Thanks,
Eryu

Quoted 4 lines
common/rc +1 -0
+
 init_rc
 
 ################################################################################
#4 lore raw

PATCH 2/3 fstests: test restricted symlinks & hardlinks sysctls

In reply to Eric Sandeen

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

---

tests/generic/900 new file +114 -0
new file mode 100755
index 00000000..f0ac46ef
@@ -0,0 +1,114 @@
+#! /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 ioctls
+#
+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
+	sysctl -qw fs.protected_symlinks=$SYMLINK_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 fs.protected_symlinks
+_require_sysctl fs.protected_hardlinks
+_require_user fsgqa
+_require_user fsgqa2
+
+OWNER=fsgqa
+OTHER=fsgqa2
+
+# 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
+	sudo -u $OTHER cat $TEST_DIR/$seq/sticky_dir/symlink 2>&1 \
+		 | _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
+	sudo -u $OTHER \
+	    ln $TEST_DIR/$seq/target $TEST_DIR/$seq/sticky_dir/hardlink 2>&1 \
+		| _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
tests/generic/900.out new file +14 -0
new file mode 100644
index 00000000..c9b26dbd
@@ -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
+cat: TEST_DIR/900/sticky_dir/symlink: 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
tests/generic/group +1 -0
index 718575ba..782b0cc3 100644
@@ -598,3 +598,4 @@
 594 auto quick quota
 595 auto quick encrypt
 596 auto quick
+900 auto quick perms
#5 lore raw

Re: [PATCH 2/3] fstests: test restricted symlinks & hardlinks sysctls

Quoted 50 lines · Eric Sandeen

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

---

tests/generic/900 new file +36 -0
new file mode 100755
index 00000000..f0ac46ef
@@ -0,0 +1,114 @@
+#! /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 ioctls
+#
+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
+	sysctl -qw fs.protected_symlinks=$SYMLINK_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.

tests/generic/900 line 46

Quoted 10 lines
tests/generic/900 new file +10 -0
+_supported_fs generic
+_supported_os Linux
+_require_test
+_require_sysctl fs.protected_symlinks
+_require_sysctl fs.protected_hardlinks
+_require_user fsgqa
+_require_user fsgqa2
+
+OWNER=fsgqa
+OTHER=fsgqa2

Why fsgqa2 instead of 123456-fsgqa?
Thanks-
Bill

Quoted 99 lines
tests/generic/900 new file +68 -0
+
+# 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
+	sudo -u $OTHER cat $TEST_DIR/$seq/sticky_dir/symlink 2>&1 \
+		 | _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
+	sudo -u $OTHER \
+	    ln $TEST_DIR/$seq/target $TEST_DIR/$seq/sticky_dir/hardlink 2>&1 \
+		| _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
tests/generic/900.out new file +14 -0
new file mode 100644
index 00000000..c9b26dbd
@@ -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
+cat: TEST_DIR/900/sticky_dir/symlink: 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
tests/generic/group +1 -0
index 718575ba..782b0cc3 100644
@@ -598,3 +598,4 @@
 594 auto quick quota
 595 auto quick encrypt
 596 auto quick
+900 auto quick perms
#6 lore raw

Re: [PATCH 2/3] fstests: test restricted symlinks & hardlinks sysctls

Quoted 1 line · Bill O'Donnell

On Tue, May 05, 2020 at 03:20:10PM -0500, Eric Sandeen wrote:

...

Quoted 6 lines

tests/generic/900 line 46

Quoted 2 lines
tests/generic/900 new file +2 -0
+OWNER=fsgqa
+OTHER=fsgqa2

Why fsgqa2 instead of 123456-fsgqa?
Thanks-
Bill

Yeah, not quite sure what to do about that. That username is somewhat specific to test generic/381 because it wants to test usernames starting with digits. I hate to proliferate required users, though, so not sure what the best way to go might be.

I could maybe/probably use root as one of the users, but didn't want to over-complicate it with matching admin privs vs. normal user privs.

-Eric

Reviewed-by
#7 lore raw

Re: [PATCH 2/3] fstests: test restricted symlinks & hardlinks sysctls

Quoted 12 lines · Eric Sandeen
Quoted 1 line · Bill O'Donnell

On Tue, May 05, 2020 at 03:20:10PM -0500, Eric Sandeen wrote:

...

Quoted 6 lines

tests/generic/900 line 46

Quoted 2 lines
tests/generic/900 new file +2 -0
+OWNER=fsgqa
+OTHER=fsgqa2

Why fsgqa2 instead of 123456-fsgqa?
Thanks-
Bill

Quoted 7 lines

Yeah, not quite sure what to do about that. That username is somewhat specific to test generic/381 because it wants to test usernames starting with digits. I hate to proliferate required users, though, so not sure what the best way to go might be.

I could maybe/probably use root as one of the users, but didn't want to over-complicate it with matching admin privs vs. normal user privs.

Fair enough. THanks!

Quoted 3 lines

-Eric

#8 lore raw

Re: [PATCH 2/3] fstests: test restricted symlinks & hardlinks sysctls

In reply to Eric Sandeen
Quoted 26 lines · Eric Sandeen

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

---

tests/generic/900 new file +12 -0
new file mode 100755
index 00000000..f0ac46ef
@@ -0,0 +1,114 @@
+#! /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 ioctls
+#
+seq=`basename $0`
+seqres=$RESULT_DIR/$seq
+echo "QA output created by $seq"
+

tests/generic/900 line 22

Quoted 10 lines
tests/generic/900 new file +10 -0
+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
+	sysctl -qw fs.protected_symlinks=$SYMLINK_PROTECTION
+	sysctl -qw fs.protected_hardlinks=$HARDLINK_PROTECTION

Restore the sysctl only if "SYMLINK_PROTECTION" and "HARDLINK_PROTECTION" are set.

Quoted 11 lines
tests/generic/900 new file +11 -0
+	cd /
+	rm -f $tmp.*
+}
+
+# get standard environment, filters and checks
+. ./common/rc
+. ./common/filter
+
+# remove previous $seqres.full before test
+rm -f $seqres.full
+

tests/generic/900 line 43

Quoted 10 lines
tests/generic/900 new file +10 -0
+# real QA test starts here
+
+# Modify as appropriate.
+_supported_fs generic
+_supported_os Linux
+_require_test
+_require_sysctl fs.protected_symlinks
+_require_sysctl fs.protected_hardlinks
+_require_user fsgqa
+_require_user fsgqa2

New user :) update README as well?

Quoted 5 lines
tests/generic/900 new file +5 -0
+
+OWNER=fsgqa
+OTHER=fsgqa2
+
+# Save current system state to reset when done

tests/generic/900 line 58

Quoted 10 lines
tests/generic/900 new file +10 -0
+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
+	sudo -u $OTHER cat $TEST_DIR/$seq/sticky_dir/symlink 2>&1 \

Use _user_do instead of sudo?

tests/generic/900 line 69

Quoted 11 lines
tests/generic/900 new file +11 -0
+		 | _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
+	sudo -u $OTHER \
+	    ln $TEST_DIR/$seq/target $TEST_DIR/$seq/sticky_dir/hardlink 2>&1 \
+		| _filter_test_dir

Same here.

Thanks,
Eryu

Quoted 75 lines
tests/generic/900 new file +45 -0
+	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
tests/generic/900.out new file +14 -0
new file mode 100644
index 00000000..c9b26dbd
@@ -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
+cat: TEST_DIR/900/sticky_dir/symlink: 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
tests/generic/group +1 -0
index 718575ba..782b0cc3 100644
@@ -598,3 +598,4 @@
 594 auto quick quota
 595 auto quick encrypt
 596 auto quick
+900 auto quick perms
#9 lore raw

Re: [PATCH 2/3] fstests: test restricted symlinks & hardlinks sysctls

Quoted 32 lines · Eryu Guan
Quoted 31 lines · Eric Sandeen

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

---

tests/generic/900 new file +17 -0
new file mode 100755
index 00000000..f0ac46ef
@@ -0,0 +1,114 @@
+#! /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 ioctls
+#
+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
+
Quoted 8 lines

tests/generic/900 line 22

Quoted 5 lines
tests/generic/900 new file +5 -0
+_cleanup()
+{
+	rm -rf $TEST_DIR/$seq
+	sysctl -qw fs.protected_symlinks=$SYMLINK_PROTECTION
+	sysctl -qw fs.protected_hardlinks=$HARDLINK_PROTECTION

Restore the sysctl only if "SYMLINK_PROTECTION" and "HARDLINK_PROTECTION" are set.

thankss

Quoted 15 lines
Quoted 15 lines
tests/generic/900 new file +15 -0
+	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
Quoted 8 lines

tests/generic/900 line 43

Quoted 6 lines
tests/generic/900 new file +6 -0
+_supported_os Linux
+_require_test
+_require_sysctl fs.protected_symlinks
+_require_sysctl fs.protected_hardlinks
+_require_user fsgqa
+_require_user fsgqa2

New user :) update README as well?

Hm, yep.

Quoted 9 lines
Quoted 9 lines
tests/generic/900 new file +9 -0
+
+OWNER=fsgqa
+OTHER=fsgqa2
+
+# Save current system state to reset when done
+SYMLINK_PROTECTION=`sysctl -n fs.protected_symlinks`
+HARDLINK_PROTECTION=`sysctl -n fs.protected_hardlinks`
+
+test_symlink()
Quoted 8 lines

tests/generic/900 line 58

Quoted 6 lines
tests/generic/900 new file +6 -0
+{
+	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
+	sudo -u $OTHER cat $TEST_DIR/$seq/sticky_dir/symlink 2>&1 \

Use _user_do instead of sudo?

but OTHER is "fsgqa2;" _user_do uses "fsgqa" .... hrm. I suppose I could update _user_do to take a username, but is it worth it?

Maybe I should just make one of the users root, I'm just worried about accidentally running into admin capabilities...

-Eric

#10 lore raw

Re: [PATCH 2/3] fstests: test restricted symlinks & hardlinks sysctls

Quoted 8 lines · Eric Sandeen
Quoted 8 lines

tests/generic/900 line 58

Quoted 7 lines
tests/generic/900 new file +7 -0
+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
+	sudo -u $OTHER cat $TEST_DIR/$seq/sticky_dir/symlink 2>&1 \

Use _user_do instead of sudo?

Quoted 5 lines

but OTHER is "fsgqa2;" _user_do uses "fsgqa" .... hrm. I suppose I could update _user_do to take a username, but is it worth it?

Maybe I should just make one of the users root, I'm just worried about accidentally running into admin capabilities...

Actually I'll just re-use 123456-fsgq for OWNER, then OTHER can be fsgqa, and I'll use _user_do.

Thanks,
-Eric

#11 lore raw

PATCH 3/3 fstests: test restricted file access sysctls

In reply to Eric Sandeen

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

---

tests/generic/901 new file +127 -0
new file mode 100755
index 00000000..5906b53c
@@ -0,0 +1,127 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2020 Red Hat, Inc.  All Rights Reserved.
+#
+# FS QA Test 900
+#
+# 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
+	sysctl -qw fs.protected_regular=$REGULAR_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 fs.protected_regular
+_require_sysctl fs.protected_fifos
+_require_user fsgqa
+_require_user fsgqa2
+
+USER1=fsgqa
+USER2=fsgqa2
+
+# 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
+	result=`sudo -u $USER2 $XFS_IO_PROG -c "open -f $TEST_DIR/$seq/sticky_dir/$FILENAME" 2>&1` \
+		&& echo "successfully opened $FILENAME" \
+		|| echo $result | _filter_test_dir
+	# sticky dir is only group writable:
+	echo "= only group writable dir"
+	chmod o-w $TEST_DIR/$seq/sticky_dir
+	result=`sudo -u $USER2 $XFS_IO_PROG -c "open -f $TEST_DIR/$seq/sticky_dir/$FILENAME" 2>&1` \
+		&& echo "successfully opened $FILENAME" \
+		|| echo $result | _filter_test_dir
+}
+
+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
tests/generic/901.out new file +34 -0
new file mode 100644
index 00000000..af774ca5
@@ -0,0 +1,34 @@
+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
+successfully opened file
+= only group writable dir
+successfully opened file
+fs.protected_regular = 1
+= group & world writable dir
+TEST_DIR/901/sticky_dir/file: Permission denied
+= only group writable dir
+successfully opened file
+fs.protected_regular = 2
+= group & world writable dir
+TEST_DIR/901/sticky_dir/file: Permission denied
+= only group writable dir
+TEST_DIR/901/sticky_dir/file: Permission denied
+
+== Test fifo open when owned by another and fifo owner != dir owner
+fs.protected_fifos = 0
+= group & world writable dir
+successfully opened fifo
+= only group writable dir
+successfully opened fifo
+fs.protected_fifos = 1
+= group & world writable dir
+TEST_DIR/901/sticky_dir/fifo: Permission denied
+= only group writable dir
+successfully opened fifo
+fs.protected_fifos = 2
+= group & world writable dir
+TEST_DIR/901/sticky_dir/fifo: Permission denied
+= only group writable dir
+TEST_DIR/901/sticky_dir/fifo: Permission denied
tests/generic/group +1 -0
index 782b0cc3..d1e529d5 100644
@@ -599,3 +599,4 @@
 595 auto quick encrypt
 596 auto quick
 900 auto quick perms
+901 auto quick perms
#12 lore raw
Quoted 50 lines · Eric Sandeen

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

---

tests/generic/901 new file +36 -0
new file mode 100755
index 00000000..5906b53c
@@ -0,0 +1,127 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2020 Red Hat, Inc.  All Rights Reserved.
+#
+# FS QA Test 900
+#
+# 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
+	sysctl -qw fs.protected_regular=$REGULAR_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.

tests/generic/901 line 46

Quoted 10 lines
tests/generic/901 new file +10 -0
+_supported_fs generic
+_supported_os Linux
+_require_test
+_require_sysctl fs.protected_regular
+_require_sysctl fs.protected_fifos
+_require_user fsgqa
+_require_user fsgqa2
+
+USER1=fsgqa
+USER2=fsgqa2

See my fsgqa2 question in previous patch. ;)
-Bill

Quoted 132 lines
tests/generic/901 new file +81 -0
+
+# 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
+	result=`sudo -u $USER2 $XFS_IO_PROG -c "open -f $TEST_DIR/$seq/sticky_dir/$FILENAME" 2>&1` \
+		&& echo "successfully opened $FILENAME" \
+		|| echo $result | _filter_test_dir
+	# sticky dir is only group writable:
+	echo "= only group writable dir"
+	chmod o-w $TEST_DIR/$seq/sticky_dir
+	result=`sudo -u $USER2 $XFS_IO_PROG -c "open -f $TEST_DIR/$seq/sticky_dir/$FILENAME" 2>&1` \
+		&& echo "successfully opened $FILENAME" \
+		|| echo $result | _filter_test_dir
+}
+
+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
tests/generic/901.out new file +34 -0
new file mode 100644
index 00000000..af774ca5
@@ -0,0 +1,34 @@
+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
+successfully opened file
+= only group writable dir
+successfully opened file
+fs.protected_regular = 1
+= group & world writable dir
+TEST_DIR/901/sticky_dir/file: Permission denied
+= only group writable dir
+successfully opened file
+fs.protected_regular = 2
+= group & world writable dir
+TEST_DIR/901/sticky_dir/file: Permission denied
+= only group writable dir
+TEST_DIR/901/sticky_dir/file: Permission denied
+
+== Test fifo open when owned by another and fifo owner != dir owner
+fs.protected_fifos = 0
+= group & world writable dir
+successfully opened fifo
+= only group writable dir
+successfully opened fifo
+fs.protected_fifos = 1
+= group & world writable dir
+TEST_DIR/901/sticky_dir/fifo: Permission denied
+= only group writable dir
+successfully opened fifo
+fs.protected_fifos = 2
+= group & world writable dir
+TEST_DIR/901/sticky_dir/fifo: Permission denied
+= only group writable dir
+TEST_DIR/901/sticky_dir/fifo: Permission denied
tests/generic/group +1 -0
index 782b0cc3..d1e529d5 100644
@@ -599,3 +599,4 @@
 595 auto quick encrypt
 596 auto quick
 900 auto quick perms
+901 auto quick perms
Reviewed-by on v1 3/3
#13 lore raw
In reply to Eric Sandeen
Quoted 7 lines · Eric Sandeen

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

---

looks good.

Quoted 185 lines
tests/generic/901 new file +127 -0
new file mode 100755
index 00000000..5906b53c
@@ -0,0 +1,127 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2020 Red Hat, Inc.  All Rights Reserved.
+#
+# FS QA Test 900
+#
+# 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
+	sysctl -qw fs.protected_regular=$REGULAR_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 fs.protected_regular
+_require_sysctl fs.protected_fifos
+_require_user fsgqa
+_require_user fsgqa2
+
+USER1=fsgqa
+USER2=fsgqa2
+
+# 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
+	result=`sudo -u $USER2 $XFS_IO_PROG -c "open -f $TEST_DIR/$seq/sticky_dir/$FILENAME" 2>&1` \
+		&& echo "successfully opened $FILENAME" \
+		|| echo $result | _filter_test_dir
+	# sticky dir is only group writable:
+	echo "= only group writable dir"
+	chmod o-w $TEST_DIR/$seq/sticky_dir
+	result=`sudo -u $USER2 $XFS_IO_PROG -c "open -f $TEST_DIR/$seq/sticky_dir/$FILENAME" 2>&1` \
+		&& echo "successfully opened $FILENAME" \
+		|| echo $result | _filter_test_dir
+}
+
+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
tests/generic/901.out new file +34 -0
new file mode 100644
index 00000000..af774ca5
@@ -0,0 +1,34 @@
+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
+successfully opened file
+= only group writable dir
+successfully opened file
+fs.protected_regular = 1
+= group & world writable dir
+TEST_DIR/901/sticky_dir/file: Permission denied
+= only group writable dir
+successfully opened file
+fs.protected_regular = 2
+= group & world writable dir
+TEST_DIR/901/sticky_dir/file: Permission denied
+= only group writable dir
+TEST_DIR/901/sticky_dir/file: Permission denied
+
+== Test fifo open when owned by another and fifo owner != dir owner
+fs.protected_fifos = 0
+= group & world writable dir
+successfully opened fifo
+= only group writable dir
+successfully opened fifo
+fs.protected_fifos = 1
+= group & world writable dir
+TEST_DIR/901/sticky_dir/fifo: Permission denied
+= only group writable dir
+successfully opened fifo
+fs.protected_fifos = 2
+= group & world writable dir
+TEST_DIR/901/sticky_dir/fifo: Permission denied
+= only group writable dir
+TEST_DIR/901/sticky_dir/fifo: Permission denied
tests/generic/group +1 -0
index 782b0cc3..d1e529d5 100644
@@ -599,3 +599,4 @@
 595 auto quick encrypt
 596 auto quick
 900 auto quick perms
+901 auto quick perms
#14 lore raw
In reply to Eric Sandeen
Quoted 6 lines · Eric Sandeen

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

Actually, I'm going to self-nak this one, sorry; this bit -

tests/generic/901 line 62

Quoted 4 lines
tests/generic/901 new file +4 -0
+	# "open -f" opens O_CREAT
+	result=`sudo -u $USER2 $XFS_IO_PROG -c "open -f $TEST_DIR/$seq/sticky_dir/$FILENAME" 2>&1` \
+		&& echo "successfully opened $FILENAME" \
+		|| echo $result | _filter_test_dir

depends on xfs_io exiting nonzero on a failure and that was only recently fixed; I'll come up with a test that's more robust and send a V2.

-Eric

#15 lore raw

PATCH v2 3/3 fstests: test restricted file access sysctls

In reply to Eric Sandeen

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

---

V2: Don't count on nonzero exit from xfs_io since that is almost always broken and was only recently fixed and may end up broken again... collect & check any emitted error messages instead.

tests/generic/901 new file +133 -0
new file mode 100755
index 00000000..9bb658e0
@@ -0,0 +1,133 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2020 YOUR NAME HERE.  All Rights Reserved.
+#
+# FS QA Test 900
+#
+# Test protected_symlink and protected_hardlink ioctls
+#
+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
+	sysctl -qw fs.protected_regular=$REGULAR_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 fs.protected_regular
+_require_sysctl fs.protected_fifos
+_require_user fsgqa
+_require_user fsgqa2
+
+USER1=fsgqa
+USER2=fsgqa2
+
+# 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
+	result=`sudo -u $USER2 $XFS_IO_PROG -c "open -f $TEST_DIR/$seq/sticky_dir/$FILENAME" 2>&1`
+	if [ -z "$result" ]; then
+		echo "successfully opened $FILENAME"
+	else
+		echo $result | _filter_test_dir
+	fi
+	# sticky dir is only group writable:
+	echo "= only group writable dir"
+	chmod o-w $TEST_DIR/$seq/sticky_dir
+	result=`sudo -u $USER2 $XFS_IO_PROG -c "open -f $TEST_DIR/$seq/sticky_dir/$FILENAME" 2>&1`
+	if [ -z "$result" ]; then
+		echo "successfully opened $FILENAME"
+	else
+		echo $result | _filter_test_dir
+	fi
+}
+
+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
tests/generic/901.out new file +34 -0
new file mode 100644
index 00000000..af774ca5
@@ -0,0 +1,34 @@
+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
+successfully opened file
+= only group writable dir
+successfully opened file
+fs.protected_regular = 1
+= group & world writable dir
+TEST_DIR/901/sticky_dir/file: Permission denied
+= only group writable dir
+successfully opened file
+fs.protected_regular = 2
+= group & world writable dir
+TEST_DIR/901/sticky_dir/file: Permission denied
+= only group writable dir
+TEST_DIR/901/sticky_dir/file: Permission denied
+
+== Test fifo open when owned by another and fifo owner != dir owner
+fs.protected_fifos = 0
+= group & world writable dir
+successfully opened fifo
+= only group writable dir
+successfully opened fifo
+fs.protected_fifos = 1
+= group & world writable dir
+TEST_DIR/901/sticky_dir/fifo: Permission denied
+= only group writable dir
+successfully opened fifo
+fs.protected_fifos = 2
+= group & world writable dir
+TEST_DIR/901/sticky_dir/fifo: Permission denied
+= only group writable dir
+TEST_DIR/901/sticky_dir/fifo: Permission denied
tests/generic/group +1 -0
index 782b0cc3..d1e529d5 100644
@@ -599,3 +599,4 @@
 595 auto quick encrypt
 596 auto quick
 900 auto quick perms
+901 auto quick perms
Reviewed-by on v2 3/3
#16 lore raw
Quoted 73 lines · Eric Sandeen

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

---

V2: Don't count on nonzero exit from xfs_io since that is almost always broken and was only recently fixed and may end up broken again... collect & check any emitted error messages instead.

tests/generic/901 new file +55 -0
new file mode 100755
index 00000000..9bb658e0
@@ -0,0 +1,133 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2020 YOUR NAME HERE.  All Rights Reserved.
+#
+# FS QA Test 900
+#
+# Test protected_symlink and protected_hardlink ioctls
+#
+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
+	sysctl -qw fs.protected_regular=$REGULAR_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 fs.protected_regular
+_require_sysctl fs.protected_fifos
+_require_user fsgqa
+_require_user fsgqa2
+
+USER1=fsgqa
+USER2=fsgqa2
+
+# 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
+

tests/generic/901 line 65

Quoted 10 lines
tests/generic/901 new file +10 -0
+	# sticky dir is world & group writable:
+	echo "= group & world writable dir"
+	chmod og+w $TEST_DIR/$seq/sticky_dir
+	# "open -f" opens O_CREAT
+	result=`sudo -u $USER2 $XFS_IO_PROG -c "open -f $TEST_DIR/$seq/sticky_dir/$FILENAME" 2>&1`
+	if [ -z "$result" ]; then
+		echo "successfully opened $FILENAME"
+	else
+		echo $result | _filter_test_dir
+	fi

Makes sense, thanks.

Quoted 118 lines
tests/generic/901 new file +68 -0
+	# sticky dir is only group writable:
+	echo "= only group writable dir"
+	chmod o-w $TEST_DIR/$seq/sticky_dir
+	result=`sudo -u $USER2 $XFS_IO_PROG -c "open -f $TEST_DIR/$seq/sticky_dir/$FILENAME" 2>&1`
+	if [ -z "$result" ]; then
+		echo "successfully opened $FILENAME"
+	else
+		echo $result | _filter_test_dir
+	fi
+}
+
+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
tests/generic/901.out new file +34 -0
new file mode 100644
index 00000000..af774ca5
@@ -0,0 +1,34 @@
+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
+successfully opened file
+= only group writable dir
+successfully opened file
+fs.protected_regular = 1
+= group & world writable dir
+TEST_DIR/901/sticky_dir/file: Permission denied
+= only group writable dir
+successfully opened file
+fs.protected_regular = 2
+= group & world writable dir
+TEST_DIR/901/sticky_dir/file: Permission denied
+= only group writable dir
+TEST_DIR/901/sticky_dir/file: Permission denied
+
+== Test fifo open when owned by another and fifo owner != dir owner
+fs.protected_fifos = 0
+= group & world writable dir
+successfully opened fifo
+= only group writable dir
+successfully opened fifo
+fs.protected_fifos = 1
+= group & world writable dir
+TEST_DIR/901/sticky_dir/fifo: Permission denied
+= only group writable dir
+successfully opened fifo
+fs.protected_fifos = 2
+= group & world writable dir
+TEST_DIR/901/sticky_dir/fifo: Permission denied
+= only group writable dir
+TEST_DIR/901/sticky_dir/fifo: Permission denied
tests/generic/group +1 -0
index 782b0cc3..d1e529d5 100644
@@ -599,3 +599,4 @@
 595 auto quick encrypt
 596 auto quick
 900 auto quick perms
+901 auto quick perms
#17 lore raw
In reply to Eric Sandeen
Quoted 18 lines · Eric Sandeen

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

---

V2: Don't count on nonzero exit from xfs_io since that is almost always broken and was only recently fixed and may end up broken again... collect & check any emitted error messages instead.

tests/generic/901 new file +0 -0
new file mode 100755
index 00000000..9bb658e0

tests/generic/901 line 3

Quoted 3 lines
tests/generic/901 new file +3 -0
@@ -0,0 +1,133 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2020 YOUR NAME HERE.  All Rights Reserved.

Copyright info is not correct :)

tests/generic/901 line 7

Quoted 4 lines
tests/generic/901 new file +4 -0
+#
+# FS QA Test 900
+#
+# Test protected_symlink and protected_hardlink ioctls

Stale test description?

Quoted 5 lines
tests/generic/901 new file +5 -0
+#
+seq=`basename $0`
+seqres=$RESULT_DIR/$seq
+echo "QA output created by $seq"
+

tests/generic/901 line 22

Quoted 10 lines
tests/generic/901 new file +10 -0
+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
+	sysctl -qw fs.protected_regular=$REGULAR_PROTECTION
+	sysctl -qw fs.protected_fifos=$FIFO_PROTECTION

Only restore them only if the variables are set.

Thanks,
Eryu

Quoted 160 lines
tests/generic/901 new file +111 -0
+	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 fs.protected_regular
+_require_sysctl fs.protected_fifos
+_require_user fsgqa
+_require_user fsgqa2
+
+USER1=fsgqa
+USER2=fsgqa2
+
+# 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
+	result=`sudo -u $USER2 $XFS_IO_PROG -c "open -f $TEST_DIR/$seq/sticky_dir/$FILENAME" 2>&1`
+	if [ -z "$result" ]; then
+		echo "successfully opened $FILENAME"
+	else
+		echo $result | _filter_test_dir
+	fi
+	# sticky dir is only group writable:
+	echo "= only group writable dir"
+	chmod o-w $TEST_DIR/$seq/sticky_dir
+	result=`sudo -u $USER2 $XFS_IO_PROG -c "open -f $TEST_DIR/$seq/sticky_dir/$FILENAME" 2>&1`
+	if [ -z "$result" ]; then
+		echo "successfully opened $FILENAME"
+	else
+		echo $result | _filter_test_dir
+	fi
+}
+
+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
tests/generic/901.out new file +34 -0
new file mode 100644
index 00000000..af774ca5
@@ -0,0 +1,34 @@
+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
+successfully opened file
+= only group writable dir
+successfully opened file
+fs.protected_regular = 1
+= group & world writable dir
+TEST_DIR/901/sticky_dir/file: Permission denied
+= only group writable dir
+successfully opened file
+fs.protected_regular = 2
+= group & world writable dir
+TEST_DIR/901/sticky_dir/file: Permission denied
+= only group writable dir
+TEST_DIR/901/sticky_dir/file: Permission denied
+
+== Test fifo open when owned by another and fifo owner != dir owner
+fs.protected_fifos = 0
+= group & world writable dir
+successfully opened fifo
+= only group writable dir
+successfully opened fifo
+fs.protected_fifos = 1
+= group & world writable dir
+TEST_DIR/901/sticky_dir/fifo: Permission denied
+= only group writable dir
+successfully opened fifo
+fs.protected_fifos = 2
+= group & world writable dir
+TEST_DIR/901/sticky_dir/fifo: Permission denied
+= only group writable dir
+TEST_DIR/901/sticky_dir/fifo: Permission denied
tests/generic/group +1 -0
index 782b0cc3..d1e529d5 100644
@@ -599,3 +599,4 @@
 595 auto quick encrypt
 596 auto quick
 900 auto quick perms
+901 auto quick perms