2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2020 Red Hat, Inc. All Rights Reserved.
7 # Test protected_symlink and protected_hardlink sysctls
10 _begin_fstest auto quick perms
12 # Override the default cleanup function.
16 [ ! -z "$SYMLINK_PROTECTION" ] \
17 && sysctl -qw fs.protected_symlinks=$SYMLINK_PROTECTION
18 [ ! -z "$HARDLINK_PROTECTION" ] \
19 && sysctl -qw fs.protected_hardlinks=$HARDLINK_PROTECTION
24 # Import common functions.
27 # real QA test starts here
29 # Modify as appropriate.
32 _require_sysctl_variable fs.protected_symlinks
33 _require_sysctl_variable fs.protected_hardlinks
35 # Do this SECOND so that qa_user is fsgqa, and _user_do uses that account
42 # Save current system state to reset when done
43 SYMLINK_PROTECTION=`sysctl -n fs.protected_symlinks`
44 HARDLINK_PROTECTION=`sysctl -n fs.protected_hardlinks`
48 ln -s $TEST_DIR/$seq/target $TEST_DIR/$seq/sticky_dir/symlink
49 chown $OTHER.$OTHER $TEST_DIR/$seq/sticky_dir
50 chown $OWNER.$OWNER $TEST_DIR/$seq/sticky_dir/symlink
51 # If we can read the target, we followed the link
52 _user_do "cat $TEST_DIR/$seq/sticky_dir/symlink" | _filter_test_dir
53 rm -f $TEST_DIR/$seq/sticky_dir/symlink
58 chown $OWNER.$OWNER $TEST_DIR/$seq/target
59 chmod go-rw $TEST_DIR/$seq/target
60 _user_do "ln $TEST_DIR/$seq/target $TEST_DIR/$seq/sticky_dir/hardlink" \
62 test -f $TEST_DIR/$seq/sticky_dir/hardlink \
63 && echo "successfully created hardlink"
64 rm -f $TEST_DIR/$seq/sticky_dir/hardlink
69 # Create world-writable sticky dir
70 mkdir -p $TEST_DIR/$seq/sticky_dir
71 chmod 1777 $TEST_DIR/$seq/sticky_dir
72 # And a file elsewhere that will be linked to from that sticky dir
73 mkdir -p $TEST_DIR/$seq
74 # If we can read it, we followed the link.
75 echo "successfully followed symlink" > $TEST_DIR/$seq/target
80 # First test fs.protected_symlinks
81 # With protection on, symlink follows should fail if the
82 # link owner != the sticky directory owner, and the process
83 # is not the link owner.
84 echo "== Test symlink follow protection when"
85 echo "== process != link owner and dir owner != link owner"
86 sysctl -w fs.protected_symlinks=0
88 sysctl -w fs.protected_symlinks=1
93 # Now test fs.protected_hardlinks
94 # With protection on, hardlink creation should fail if the
95 # process does not own the target file, and the process does not have
96 # read-write access to the target
97 echo "== Test hardlink create protection when"
98 echo "== process != target owner and process cannot read target"
99 sysctl -w fs.protected_hardlinks=0
101 sysctl -w fs.protected_hardlinks=1