smack_file_access.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/sh
  2. #
  3. # Copyright (c) 2009 Casey Schaufler under the terms of the
  4. # GNU General Public License version 2, as published by the
  5. # Free Software Foundation
  6. #
  7. # Test setting access rules
  8. #
  9. # Environment:
  10. # CAP_MAC_ADMIN
  11. #
  12. # "%-23s %-23s %4s"
  13. #
  14. # 1 2 3 4 5 6
  15. # 123456789012345678901234567890123456789012345678901234567890123456789
  16. export TCID=smack_file_access
  17. export TST_TOTAL=1
  18. . test.sh
  19. . smack_common.sh
  20. cleanup()
  21. {
  22. tst_rmdir
  23. }
  24. rule_a="TheOne TheOther r---"
  25. rule_b="TheOne TheOther rw--"
  26. CAT=/bin/cat
  27. testfile="testfile"
  28. tst_tmpdir
  29. TST_CLEANUP=cleanup
  30. smack_notroot /bin/sh -c "echo InitialData 2>/tmp/smack_fail.log > $testfile"
  31. if [ ! -f "$testfile" ]; then
  32. tst_brkm TFAIL "Test file \"$testfile\" can not be created."
  33. fi
  34. setfattr --name=security.SMACK64 --value=TheOther "$testfile"
  35. setto=$(getfattr --only-values -n security.SMACK64 -e text $testfile)
  36. if [ "TheOther" != "$setto" ]; then
  37. tst_brkm TFAIL "Test file \"$testfile\" labeled \"$setto\" incorrectly."
  38. fi
  39. old_rule=$(grep "^TheOne" "$smackfsdir/load" 2>/dev/null | grep ' TheOther ')
  40. echo -n "$rule_a" > "$smackfsdir/load"
  41. new_rule=$(grep "^TheOne" "$smackfsdir/load" 2>/dev/null | grep ' TheOther ')
  42. if [ "$new_rule" = "" ]; then
  43. tst_brkm TFAIL "Rule did not get set."
  44. fi
  45. mode=$(echo $new_rule | sed -e 's/.* //')
  46. if [ "$mode" != "r" ]; then
  47. tst_brkm TFAIL "Rule \"$new_rule\" is not set correctly."
  48. fi
  49. echo TheOne 2>/dev/null > /proc/self/attr/current
  50. got_read=$(smack_notroot $CAT "$testfile")
  51. if [ "$got_read" != "InitialData" ]; then
  52. tst_brkm TFAIL "Read failed for \"$testfile\" labeled \"TheOther\"."
  53. fi
  54. echo NotTheOne 2>/dev/null > /proc/self/attr/current
  55. got_read=$(smack_notroot $CAT "$testfile" 2> /dev/null)
  56. if [ "$got_read" = "InitialData" ]; then
  57. tst_brkm TFAIL "Read should have failed for \"$testfile\" labeled" \
  58. "\"TheOther\"."
  59. fi
  60. echo -n "$rule_b" 2>/dev/null > "$smackfsdir/load"
  61. new_rule=$(grep "^TheOne" $smackfsdir/load 2>/dev/null | grep ' TheOther ')
  62. if [ "$new_rule" = "" ]; then
  63. tst_brkm TFAIL "Rule did not get set."
  64. fi
  65. mode=$(echo $new_rule | sed -e 's/.* //')
  66. if [ "$mode" != "rw" ]; then
  67. tst_brkm TFAIL "Rule \"$new_rule\" is not set correctly."
  68. fi
  69. if [ "$old_rule" != "$new_rule" ]; then
  70. tst_resm TINFO "Notice: Test access rule changed from \"$old_rule\"" \
  71. "to \"$new_rule\"."
  72. fi
  73. tst_resm TPASS "Test \"$TCID\" success."
  74. tst_exit