fail-eperm.xpl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/perl -Tw
  2. # Ensure that rm gives the expected diagnostic when failing to remove a file
  3. # owned by some other user in a directory with the sticky bit set.
  4. # Copyright (C) 2002-2018 Free Software Foundation, Inc.
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. use strict;
  16. (my $ME = $0) =~ s|.*/||;
  17. my $uid = $<;
  18. # skip if root
  19. $uid == 0
  20. and CuSkip::skip "$ME: can't run this test as root: skipping this test";
  21. my $verbose = $ENV{VERBOSE} && $ENV{VERBOSE} eq 'yes';
  22. # Ensure that the diagnostics are in English.
  23. $ENV{LC_ALL} = 'C';
  24. # Set up a safe, well-known environment
  25. $ENV{IFS} = '';
  26. # Taint checking requires a sanitized $PATH. This script performs no $PATH
  27. # search, so on most Unix-based systems, it is fine simply to clear $ENV{PATH}.
  28. # However, on Cygwin, it's used to find cygwin1.dll, so set it.
  29. $ENV{PATH} = '/bin:/usr/bin';
  30. my @dir_list = qw(/tmp /var/tmp /usr/tmp);
  31. my $rm = "$ENV{abs_top_builddir}/src/rm";
  32. # Untaint for upcoming popen.
  33. $rm =~ m!^([-+\@\w./]+)$!
  34. or CuSkip::skip "$ME: unusual absolute builddir name; skipping this test\n";
  35. $rm = $1;
  36. # Find a directory with the sticky bit set.
  37. my $found_dir;
  38. my $found_file;
  39. foreach my $dir (@dir_list)
  40. {
  41. if (-d $dir && -k _ && -r _ && -w _ && -x _)
  42. {
  43. $found_dir = 1;
  44. # Find a non-directory there that is owned by some other user.
  45. opendir DIR_HANDLE, $dir
  46. or die "$ME: couldn't open $dir: $!\n";
  47. foreach my $f (readdir DIR_HANDLE)
  48. {
  49. # Consider only names containing "safe" characters.
  50. $f =~ /^([-\@\w.]+)$/
  51. or next;
  52. $f = $1; # untaint $f
  53. my $target_file = "$dir/$f";
  54. $verbose
  55. and warn "$ME: considering $target_file\n";
  56. # Skip files owned by self, symlinks, and directories.
  57. # It's not technically necessary to skip symlinks, but it's simpler.
  58. # SVR4-like systems (e.g., Solaris 9) let you unlink files that
  59. # you can write, so skip writable files too.
  60. -l $target_file || -o _ || -d _ || -w _
  61. and next;
  62. $found_file = 1;
  63. # Invoke rm on this file and ensure that we get the
  64. # expected exit code and diagnostic.
  65. my $cmd = "$rm -f -- $target_file";
  66. open RM, "$cmd 2>&1 |"
  67. or die "$ME: cannot execute '$cmd'\n";
  68. my $line = <RM>;
  69. close RM;
  70. my $rc = $?;
  71. # This test opportunistically looks for files that can't
  72. # be removed but those files may already have been removed
  73. # by their owners by the time we get to them. It is a
  74. # race condition. If so then the rm is successful and our
  75. # test is thwarted. Detect this case and ignore.
  76. if ($rc == 0)
  77. {
  78. next if ! -e $target_file;
  79. die "$ME: unexpected exit status from '$cmd';\n"
  80. . " got 0, expected 1\n";
  81. }
  82. if (0x80 < $rc)
  83. {
  84. my $status = $rc >> 8;
  85. $status == 1
  86. or die "$ME: unexpected exit status from '$cmd';\n"
  87. . " got $status, expected 1\n";
  88. }
  89. else
  90. {
  91. # Terminated by a signal.
  92. my $sig_num = $rc & 0x7F;
  93. die "$ME: command '$cmd' died with signal $sig_num\n";
  94. }
  95. my $exp = "rm: cannot remove '$target_file':";
  96. $line
  97. or die "$ME: no output from '$cmd';\n"
  98. . "expected something like '$exp ...'\n";
  99. # Transform the actual diagnostic so that it starts with "rm:".
  100. # Depending on your system, it might be "rm:" already, or
  101. # "../../src/rm:".
  102. $line =~ s,^\Q$rm\E:,rm:,;
  103. my $regex = quotemeta $exp;
  104. $line =~ /^$regex/
  105. or die "$ME: unexpected diagnostic from '$cmd';\n"
  106. . " got $line"
  107. . " expected $exp ...\n";
  108. last;
  109. }
  110. closedir DIR_HANDLE;
  111. $found_file
  112. and last;
  113. }
  114. }
  115. $found_dir
  116. or CuSkip::skip "$ME: couldn't find a directory with the sticky bit set;"
  117. . " skipping this test\n";
  118. $found_file
  119. or CuSkip::skip "$ME: couldn't find a file not owned by you\n"
  120. . " in any of the following directories:\n @dir_list\n"
  121. . "...so, skipping this test\n";