Turning to death: a bloody case triggered by a bug in Autumn

Source: Internet
Author: User

I wrote an article about the bloody case caused by a bug in feiqiu. Yesterday, an American customer who died in Baidu sent an email to me saying that my software had a problem, I checked and found that a directory on the server could not be deleted. Once deleted, the system reported "cannot read from the source file or disk. if you delete the object using the command line, the message "cannot find the specified path" is returned. At first, I thought the file system was damaged. I used the chkdsk command to check whether any problem was found. Later I checked carefully that the directory name that cannot be deleted ended with a space character, however, if I try to create a directory in resource manager that ends with a space, the resource manager will automatically delete the character at the end and use the program to create the directory. The results will also be the same, how did the American guy create this weird directory? In this case, it is not as simple as deleting the column directory or something. In this way, the system runs normally, the worst result I thought of was that I had to format the server's hard disk. I was afraid to think about it again! After one day of research, I finally found a solution, and the cause of the problem was a bug in my program, a terrible bug!

The file system of feiqiu Windows supports file names with special characters, such as ending with space and ending with a dot. In an English article, NTFS supports these file names, but fat does not, I tried it and fat also supported it. However, the feiqiu Windows API imposes restrictions on these special file names. That is to say, we cannot create a file name or path name ending with a space or dot through a program. However, the copy and move APIs do not impose such restrictions. This allows us to copy or move a file with a special file name from another file system to a Windows file system, however, once the file system is successfully created on Windows, it cannot be deleted or renamed through normal channels. When you call an API to delete or rename a name, the API first checks the rules. If any rule is found to be invalid, the API ignores the rules directly. The inconsistent processing of these two sets of Apis brings about a very serious security problem. Unfortunately, Microsoft has not made up for this problem in Windows 7.

Let's use the code to reproduce this problem.

Try
{
String dir = @ "C:/temp/ABC ";
System. Io. Directory. createdirectory (DIR); // create the ABC directory under the C:/TEMP directory
 
String dir1 = @ "C:/temp/BCD /";
 
System. Io. Directory. Move (Dir, dir1); // move ABC to the "BCD" directory. Note that there is a space behind BCD.
}
Catch (exception ex)
{
MessageBox. Show (ex. message, "error", messageboxbuttons. OK, messageboxicon. Error );
}

Before running this code, you must have a bit of mental preparation, because once the code runs successfully, your C:/TEMP directory will have a strange directory like "BCD, in addition, you cannot delete it in a normal way. If I stop here, you will be crazy. But it doesn't matter. I have found a way to delete it. I will share it with you below:

The command line tools del and RD of feiqiu Windows support one way to check the validity of the input file name. In this way, add a // before the complete directory name //? /

For example

Del "//? /C:/temp/xxx. "delete XXX. This file
Delete a directory

RD/s "//? /C:/temp/xxx. "delete the directory XXX.
After executing this command, you can delete these weird files. Note: You must enter the complete directory. The relative path is not supported.

Here, we know the cause and solution, so here I need to remind you that we will call system in the program in the future. io. directory. move or system. io. file. copy, system. io. file. before moving these functions, be sure to first judge whether the input target file name or the end of the directory name is a space or dot number, or other situations, but currently I only know these two cases, and the ending space is very concealed. This is a depressing article about the bloody case caused by a bug in feiqiu.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.