fstests.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eric Sandeen <sandeen@redhat.com>
To: fstests <fstests@vger.kernel.org>
Subject: [PATCH 3/5] fstests: test restricted file access sysctls
Date: Mon, 18 May 2020 11:15:34 -0500	[thread overview]
Message-ID: <9de038fc-1c35-db0b-3576-cd4bfbe4a70f@redhat.com> (raw)
In-Reply-To: <91639515-d639-c155-7cc8-660536b75257@redhat.com>

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



  parent reply	other threads:[~2020-05-18 16:15 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Eric Sandeen [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9de038fc-1c35-db0b-3576-cd4bfbe4a70f@redhat.com \
    --to=sandeen@redhat.com \
    --cc=fstests@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).