keyctl01.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0-or-later
  3. # Copyright (c) 2017 Fujitsu Ltd.
  4. # Ported: Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
  5. #
  6. # This is a regression test about potential uninitialized variable,
  7. # the test can crash the buggy kernel, and the bug has been fixed in:
  8. #
  9. # commit 38327424b40bcebe2de92d07312c89360ac9229a
  10. # Author: Dan Carpenter <dan.carpenter@oracle.com>
  11. # Date: Thu Jun 16 15:48:57 2016 +0100
  12. #
  13. # KEYS: potential uninitialized variable
  14. TST_SETUP=setup
  15. TST_CLEANUP=cleanup
  16. TST_TESTFUNC=do_test
  17. TST_NEEDS_ROOT=1
  18. TST_NEEDS_TMPDIR=1
  19. TST_NEEDS_CMDS="keyctl"
  20. . tst_test.sh
  21. check_keyctl()
  22. {
  23. local nosup
  24. for op in $@; do
  25. nosup=0
  26. if ! keyctl 2>&1 | grep -q "keyctl $op"; then
  27. nosup=1
  28. fi
  29. if [ "$op" = "request2" ]; then
  30. local key=`keyctl request2 user debug:foo bar`
  31. if [ $? -ne 0 ]; then
  32. nosup=1
  33. fi
  34. fi
  35. if [ "$op" = "unlink" ]; then
  36. if ! keyctl unlink $key @s; then
  37. nosup=1
  38. fi
  39. fi
  40. if [ $nosup -ne 0 ]; then
  41. tst_brk TCONF "keyctl operation $op not supported"
  42. fi
  43. done
  44. }
  45. setup()
  46. {
  47. check_keyctl negate request2 show unlink
  48. PATH_KEYSTAT="/proc/key-users"
  49. PATH_KEYQUOTA="/proc/sys/kernel/keys/root_maxbytes"
  50. if [ ! -f "$PATH_KEYSTAT" ] || [ ! -f "$PATH_KEYQUOTA" ]; then
  51. tst_brk TCONF "'${PATH_KEYSTAT}' or '${PATH_KEYQUOTA}' \
  52. does not exist"
  53. fi
  54. ORIG_KEYSZ=`awk -F' +|/' '/ 0:/ {print $8}' $PATH_KEYSTAT`
  55. ORIG_MAXKEYSZ=`cat $PATH_KEYQUOTA`
  56. }
  57. cleanup()
  58. {
  59. if [ -n "$ORIG_MAXKEYSZ" ]; then
  60. echo $ORIG_MAXKEYSZ >$PATH_KEYQUOTA
  61. fi
  62. }
  63. do_test()
  64. {
  65. local quota_excd=0
  66. local maxkeysz=$((ORIG_KEYSZ + 100))
  67. while [ $maxkeysz -ge 0 ]
  68. do
  69. echo $maxkeysz >$PATH_KEYQUOTA
  70. keyctl request2 user debug:fred negate @t >temp 2>&1
  71. grep -q -E "quota exceeded" temp
  72. if [ $? -eq 0 ]; then
  73. quota_excd=1
  74. break
  75. fi
  76. local key=`keyctl show | awk '/debug:fred/ {print $1}'`
  77. if [ -z "$key" ]; then
  78. key=`keyctl show | \
  79. awk -F ':' '/inaccessible/ {print $1}'`
  80. fi
  81. if [ -n "$key" ]; then
  82. keyctl unlink $key @s >/dev/null
  83. tst_sleep 50ms
  84. fi
  85. maxkeysz=$((maxkeysz - 4))
  86. done
  87. if [ $quota_excd -eq 0 ]; then
  88. tst_res TWARN "Failed to trigger the quota excess"
  89. fi
  90. tst_res TPASS "Bug not reproduced"
  91. }
  92. tst_run