rmdir() bug with particular filename? Permission denied

640 views Asked by At

First, let's get these out of the way:

  • There are no open handles on the directory.
  • There are no files in the directory.
  • chmod ing the permissions to 0777 does not prevent the error.
  • the directory name is correct.

Now then, here's my problem. rmdir() is throwing this error when trying to delete the directory:

rmdir(098f6bcd4621d373cade4e832627b4f6) [function.rmdir]: Permission denied in path\to\administrate.php on line 124

098f6bcd4621d373cade4e832627b4f6 is the name of the directory.

Here is the relevant portion of the script.

if(is_dir($userhash)) :
foreach (new DirectoryIterator($userhash) as $fileInfo) {
    $fileName = $fileInfo->getFilename();
    if($fileInfo->isDot()) continue;

    if(!rename($userhash.'/'.$fileName , 'trashcan/'.$username.'/'.$fileName)) {
        echo '<p class="error">Could not move '.$fileName.'</p>';
        $err = 1;
    }
}
else :
    echo '<p class="error">Unable to delete files! error: 67</p>';
    $err = 1;
endif;

    //JUST TO BE SURE
    chmod('./',0777);
    chmod($userhash,0777);

// RMDIR ONCE THE DIR IS EMPTY.
if(rmdir($userhash))
    echo '<p class="success">Deleted the user directory. The files are in the trash.</p>';
else {
    echo '<p class="error">Could not remove the user directory. Error: 656</p>';
    $err = 1;
}

Update

I manually created the dir 'jake' in the same directory. I did rmdir('jake'); and it worked great. Now, I manually created a dir '098f6bcd4621d373cade4e832627b4f6' in the same directory. I did rmdir('098f6bcd4621d373cade4e832627b4f6'); and it errored!

Update 2

This is beginning to look like some weird rmdir() bug, as unlikely as that seems. Here are directory names I've created and then tried to remove with rmdir;

098f6bcd4621d373cade4e832627b4f6        |  didn't work (quintuple checked)
098f6bcd4621d373cade4e832627b4f7        |  worked
098f6bcd4621d373cade4e832627b4f         |  worked
098f6bcd4621d373cade4e832627b4f66       |  worked
1

There are 1 answers

6
Kel On

In order to be able remove file:

  • user should have write permissions to PARENT directory
  • restricted deletion flag should NOT be set for PARENT directory

UPDATE: About restricted deletion flag - from man chmod:

RESTRICTED DELETION FLAG OR STICKY BIT

The restricted deletion flag or sticky bit is a single bit, whose interpretation depends on the file type. For directories, it prevents unprivi‐leged users from removing or renaming a file in the directory unless they own the file or the directory; this is called the restricted deletion flag for the directory, and is commonly found on world-writable directories like /tmp. For regular files on some older systems, the bit saves the program's text image on the swap device so it will load more quickly when run; this is called the sticky bit.

You may SET it by adding 1 to the first octal digit in mode, for example:

chmod 1xxx dirname

UPDATE 2:

Does user, under which php is executed, has permissions to chmod parent directory?

In other words, are you sure that first chmod call returns true?

chmod('./',0777);