Linux is a secure operating system, she is based in a file, its file permissions are more complex, you can use the stat command and lsattr command to display the details of a file: $ statfile1 file: would size: file1 ' 11904blocks: 24ioblock: 4096regularile device: 301h/769dinode: 355982links: 1 access: (0755/-rwxr-xr-x) uid: (503/jack) gid: (503/general) access: 2003-10-1909: 14: 12.000000000 + 0800 modify: 2003-10-1420: 41: 21.000000000 + 0800 change: 2003-10-18: 56: 25.000000000 + 0800 $ lsattrile----i--a-----ile can see that the file permissions of meaning is broad, take a look-rwxr-xr-x, first is the file type, which defines the user can only be a way to manipulate file, followed by nine is a file access control information, Linux file permissions mechanism will users fall into three categories: the owner of the file u (user), files in the case of group g (group) and other user o (other).
Three distinct types of users can file has three different levels of permissions: read-r (read), w (write) and x (execute). Thus forming a nine permissions information, divided into three groups, one for u, g, o. In addition, the user can also set the setuid and setgid bits to change the program's execution. Use lsattr command you can see the file's properties, the control bits including asacddiijsttu, these are also able to control file access. Because of space limitations, it is not possible to analyze these one by one, this article focuses on analysis of file permissions in the w (write) really means, dig out hidden details behind it, trying to make the reader correctly using this key permission bits, not in the system management appear less leakage. In order to be more intuitive, this article uses the experimental operation manner, step by step for analysis. In order to simplify operations, we use o (other) this group permissions to do the experiment. Experiment with permission bits are o (other), the users are non-root user, belong to o (other). In the experiment, you must clarify a concept, the directory is a file, it mainly covers two aspects of the information, the file name of the file and the file inode numbers, they have a one-to-one relationship. However directory files are special, you cannot use conventional methods for read and write, you must use a system of special commands to the operation. The command ls is actually on the catalog file for read operations, command mv, rm is on the table of contents file for write operations. Well, the talk about the w (write) the meaning of a Word, linux file permissions in the w is the file * content * for qualified. The following tests can validate. Experiment 1, directory file:/test (rwx), a plain file:/test/file (r--). the current directory:/test $ echo "abc" > ile bash: file: permissiondenied attempt to overwrite the contents of the file, the file permissions are read-only, it is clear that the operation failed. $catile helloworld! No problem, you can read the contents of the file. $ Mvfilefile2 $ ls file2 what's going on?, file permissions it is read-only, please note that the previously mentioned, rw permissions in the file is specific to the current contents of the file name that does not qualify, belonging to the current contents of the file, it is saved in the directory one level above the contents of the file. The mv command is for a file on the surface, actually is to overwrite the contents of/test, take a look at the permissions,/test is writable (rwx). Many users in order to protect the files on which the permission is set to read-only, no matter what, this is very dangerous, of course, you can achieve the purpose of protecting the contents of the file (in fact, it may not be to supplement content has dealt with), but you cannot guarantee that files will be renamed or deleted, the insurance is the file permissions of the parent directory is set to read-only. Of course, there are other methods, such as using chattr + I command or file system mounted with ro way and so on, but these are not discussed in this article. $ Rm-ffile2 $ ls $ for the same reason, we can remove file2, and the last operation is different is that we do not overwrite a record/test, instead, remove a record/test. Experiment 2, catalog files:/test (r-x), a plain file:/test/file (rw-), the current directory:/test $ echo "abc" > ile $ catile abc/test permissions while is read-only, but we overwrite the contents of the file, it is writable, and, of course, no problem. $ Mvfilefile2 mv: cannotmove'file'to'file2 ': permissiondenied $ rm-file rm: cannotremove'file ': permissiondenied we already know, these two instructions in fact and filRegardless of the permissions e, but overwrite the content of course/test operation failed. Through several actions before, we should distinguish the real object of operation instructions, so you can make to the file permissions are set correctly. Experiment 3, catalog files:/test (rwx), a plain file:/test/file (r--), catalog files:/test/dir (r-x), a plain file:/test/dir/file (rw-), the current directory:/test $ mvfilefile2 $ mvdirdir2 $ ls dir2file2 smoothly, because the parent/test/file and/test/dir/test permissions to the directory too lenient, rwx. $ Rm-ffile2 $ rm-rfdir2 rm: cannotremove'dir2/file ': permissiondenied $ ls-r.: dir2./dir2: ile to here, we have absolutely no surprise ordinary file/test/file2 deleted, but have the same status of the catalog file but unharmed./test/dir2 When performing rm-rfdir2, due to an ordinary file system then try/test/dir2/file, first remove it, which is equivalent to modify the contents of the directory file/test/dir2, but it is read-only and cannot be modified, that is the equivalent of not able to delete/test/dir2/file, and since there are/test/dir2/file/test/dir2 and dependencies, it will naturally/test/dir2 is preserved. Recalling the last action (mvdirdir2), why the catalog file can be renamed/test/dir? since renamed operation does not involve their own content is modified, the modified only the contents of the parent directory, and delete operations, the parent directory's content is to be modified, but also to modify its content (because you want to delete the file), this is not allowed. If permission is/test/dir2 can write, or directory without a file, then it's off, on and/test/file2 is deleted. Through the preceding several operations, you can see that the files are there so few key State: read, rewritten and renamed, deleted, is executed. However, the system only distinguish between three types of permissions, read, write and execute (rwx). Then rename and delete the two operations are system ignored? no, the system will fall into the two actions are actions file directory to manage. It is how to manage the? the answer is in the directory it of all files as its content. Then, when the user changed the name, or delete a file, executing a directory of write operations that belong to one of the three kinds of permissions rwx w operation, did not escape the system of management. Our brains are always active, to imagine all sorts of things that can make a lot of simple things into a complex thing, several experiments not exactly right, as in this experiment we can also design a lot, but do more brain seems to be more chaos (I already have one), you can remember the lives of so many? ok, we might be able to move it to a number of simple, only need to pay attention to two aspects, one is to clear the contents of the directory is nothing; the second is to understand file permissions in the w (write) really means. Think about it, right? Added: 1 experiment, if you use vim for editing the file, and force save (w!), can be successful. This is not to say that vim you can bypass the system's security mechanisms, but vim play a little trick, it is to delete the file, and generates a new file with the same name. But there is one exception, that is, when this file is also a hard link files exist, vim refuses to force save, think about it, when a delete operation, the file still exists, and is not actually deleted, but then create a new file, although with the same name, but was not the original author of that file! had on this subject is also quite puzzled, in order to verify, carefully read the source code before vim6.2 find answers. Interested readers can take a look at the specific content in the src/fileio.c.
No comments:
Post a Comment